TakahikoKawasaki / nv-websocket-client

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

The end of the stream has been reached unexpectedly. #63

Closed umesh0492 closed 8 years ago

umesh0492 commented 8 years ago

Non-fatal Exception: com.neovisionaries.ws.client.WebSocketException: The end of the stream has been reached unexpectedly. at com.neovisionaries.ws.client.WebSocketInputStream.readBytes(WebSocketInputStream.java:153) at com.neovisionaries.ws.client.WebSocketInputStream.readFrame(WebSocketInputStream.java:44) at com.neovisionaries.ws.client.ReadingThread.readFrame(ReadingThread.java:334) at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:100) at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:65)

TakahikoKawasaki commented 8 years ago

@umesh0492 This may be a problem in this WebSocket library, but basically, this problem happens when a WebSocket server closes a WebSocket connection without performing a closing handshake (RFC 6455, 7. Closing the Connection). Please see also #12 .

ghost commented 8 years ago

@TakahikoKawasaki Would a network interruption cause the same issue? Is there any way I can detect if the server doesn't perform proper closing handshake or there is actually network interruption which causes this error ?

umesh0492 commented 8 years ago

@TakahikoKawasaki i am sending the close frame to server when a connection close.

TakahikoKawasaki commented 8 years ago

@l1f3l10n Network interruption will raise a WebSocketException and your WebSocketListener can detect it. In some cases, however, underlying systems such as Android do not report network disconnection to upper layers. In such a case, WebSocket library cannot detect the issue. See also my second comment in #55 .

ghost commented 8 years ago

@TakahikoKawasaki Saw your solution in other post. Thanks for your reply.

TakahikoKawasaki commented 8 years ago

@umesh0492 The spec (RFC 6455) says both a client and a server can initiate a closing handshake by sending a close frame and the opposite which receives a close frame must send back a close frame to the initiating sender. I'm wondering whether your WebSocket server sends back a close frame.

One improvement idea is to treat an empty frame in a graceful way instead of raising an WebSocketException, but I'm not so sure it is a good option. If I had to implement it, I would add a flag to enable/disable the feature. (But adding the feature is not a simple task.)

rajashekarvys commented 8 years ago

I am facing same issue in particular android device(Micro max Yu Yuphoria Yu5010A ) not in every device , is there any solution for this.

TakahikoKawasaki commented 8 years ago

@rajashekarvys Hmm... It seems I should modify the implementation to work around this issue. Please give me some time.

rajashekarvys commented 8 years ago

Okay thank you

TakahikoKawasaki commented 8 years ago

Released the version 1.29 for this issue and #12. 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.