TakahikoKawasaki / nv-websocket-client

High-quality WebSocket client implementation in Java.
Apache License 2.0
2.03k stars 293 forks source link

Socket is getting disconnected if there is case of empty frame #12

Closed mfsi-ankitb closed 8 years ago

mfsi-ankitb commented 9 years ago

I got an issue and debugged it "socket was getting disconnected due to the fact the library is trying to read incoming frame which might not have any bytes available".

Easy proposed solution: Following lines

if (count <= 0)
{
// The end of the stream has been reached unexpectedly.
throw new WebSocketException(WebSocketError.INSUFFICENT_DATA,"The end of the stream has been reached unexpectedly.");
}

in WebSocketInputStream.java with in readBytes() method at line 141 should be commented out.

TakahikoKawasaki commented 9 years ago

Thank you for your suggestion. I'm busy now but will surely look into this later. Thanks!

TakahikoKawasaki commented 9 years ago

It seems that the issue happens when the WebSocket server closes the WebSocket connection without sending a close frame (other cases are actually "insufficient data"). If it is the case, it means that the WebSocket server does not comply with RFC 6455. What WebSocket server are you using?

mfsi-ankitb commented 8 years ago

Server is written in node.js

TakahikoKawasaki commented 8 years ago

My guess is that the behavior is caused by a wrong implementation on the server side. But, it may be doable to add an option to WebSocket class to allow an empty frame and close the connection without raising an exception. However, the modification would not be so simple as you may guess because it would be an exceptional behavior against the closing handshake and of course would be a violation against the specification. If you want to such an option, please let me know.

TakahikoKawasaki commented 8 years ago

I found that a certain WebSocket server implementation (echo.websocket.org) closes a WebSocket connection without performing a closing handshake if a WebSocket client does not immediately respond to a PING frame sent from the server.

Disconnection without a closing handshake causes nv-websocket-client to raise the error of "The end of the stream has reached unexpectedly."

Therefore, I improved the implementation to process PING & PONG frames immediately. After the improvement, the error has not been observed at least in my environment.

Please use the new version 1.16. It includes the improvement.

mfsi-ankitb commented 8 years ago

Thanks for updating, I will surely try and tell my experience

TakahikoKawasaki commented 8 years ago

Released the version 1.29 for this issue and #63. Below is "Missing Close Frame" section added to README.md. Note that "missing close frame" is not regarded as an error by default (but triggers closing handshake).


Missing Close Frame

Some server implementations close a WebSocket connection without sending a close frame to a client in some cases. Strictly speaking, this is a violation against the specification (RFC 6455). However, this library has allowed the behavior by default since the version 1.29. Even if the end of the input stream of a WebSocket connection were reached without a close frame being received, it would trigger neither onError method nor onFrameError method of WebSocketListener. If you want to make a WebSocket instance report an error in the case, pass false to setMissingCloseFrameAllowed method.

// Make this library report an error when the end of the input stream
// of the WebSocket connection is reached before a close frame is read.
ws.setMissingCloseFrameAllowed(false);
TakahikoKawasaki commented 8 years ago

If you find bugs in the implementation for "Missing Close Frame", please let me know.