dmccuskey / dmc-websockets

Corona SDK client library for the WebSocket protocol (RFC6455)
MIT License
11 stars 7 forks source link

Websocket disconnects randomly #5

Open cacharrin opened 8 years ago

cacharrin commented 8 years ago

Hi David,

I'm having problems with the library, i'm using it on CoronaSDK, but socket disconnects randomly. I just enable some logs on the library. I will tell you what I think is causing the error, but really I don't know what is causing it. But maybe my info could be useful.

I'm testing with a server which I send data to it, then it returns the same data many times (about 1500 times)... so if I send "1", it returns "111111..." ("1" character 1500 times).

I saw on the logs that sometimes the "data" comes "splitted" in many parts... when the library finally receive all parts, an ONCLOSE is fired. When data is fully available and received "complete" ONCLOSE is not fired.

A little explanation here: question

Can you check it please? What is causing this problem?

Thank you

PD: I attached the full log files log.txt

You can see for example

Every time a message is received partially, and then the second part, below the binary data there is an ONCLOSE.

dmccuskey commented 8 years ago

hello cacharrin,

the websocket protocol allows a sent message to be split up into different packets and recombined later. for some reason, the recombination part is failing.

this process starts in function handleWSFrame():

        if fcode == ws_types.continuation then
            if not self:_insertFrameData( data ) then

it would appear that inside of _insertFrameData() one of those conditionals is evaluating to true which causes the function to return nil, and that causes handleWSFrame() to close the connection.

    -- there is no type for this frame and none from previous
    if ftype == nil and #frame.data == 0 then
        return nil
    end
    -- we already have a type/data from previous frame
    if ftype ~= nil and #frame.data > 0 then
        return nil
    end

please look and see which part is returning nil.

what websocket server are you using ?

also, looking around, i can't see where the frame is reset in case of error. (by calling self:_createNewFrame()). maybe a good place would be in self:_onOpen() which is called when the socket is connected.

cacharrin commented 8 years ago

Hi David, sorry for the late response.

After some research, we found that the CLOSE event is generated by this conditional (Red square in the image) image This is on "dmc_corona/dmc_websockets/frame.lua" file.

Do you have any ideas why this could be happening?

cacharrin commented 8 years ago

David?

dmccuskey commented 8 years ago

hmm, not sure about that part. seems like the conditional may be incorrect given the error message listed.

https://tools.ietf.org/html/rfc6455#section-5.5 "All control frames MUST have a payload length of 125 bytes or less and MUST NOT be fragmented""

what happens when you comment-out that area in the red box ?

cacharrin commented 8 years ago

Thank you David, commenting those lines improves the connection stability :)

Now we have few disconnections... we will investigate further.

Thank you.

dmccuskey commented 8 years ago

it's been awhile since i researched the protocol ; i'm rather surprised that the Autobahn tests didn't catch that.

i'm curious, what websocket server are you using ?

cacharrin commented 8 years ago

Im using nodeJs + SocketIO

arash9900 commented 6 years ago

@cacharrin @dmccuskey I encountered the same problem, Did you solve this?

arash9900 commented 6 years ago

I commented the above red square, but now i got "Received reserved control frame" I checked that opcode value is 11 and fin is false in this condition. So whats the problem? I am using nodejs as server

arash9900 commented 6 years ago

I did not find any answer , so i switched to socket in corona with this library:

https://github.com/Overtorment/NoobHub