TakahikoKawasaki / nv-websocket-client

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

onDisconnect/onError not called #113

Open sacOO7 opened 7 years ago

sacOO7 commented 7 years ago
    You have made an awesome websocket client and I appreciate it, but there is a little imp. problem.

I think most of the people must have opened this type of issue. Whenever I shut down my wifi and internet is not available, dis-connect event is not getting fired. This is really a serious issue and must be solved. It's the primiary basic feature provided by socket and must call disconnect event when network goes off. The issue is not just in android but also in simple cmd APIs. I think it works perfectly in OkHTTP, Then it should also be supported in this library.

sacOO7 commented 7 years ago

This is really necessary. I know you can do it, need just little change in the code. Other options you suggested earlier just doesn't work on any platform. They are just temporary solutions.

TakahikoKawasaki commented 7 years ago

Please point to the source code in OkHttp which detects disconnection of WiFi. I want to know how OkHttp achieves it. Also, please send me a pull request containing the little change that you think can detect disconnection of WiFi.

sacOO7 commented 7 years ago

Hey, I think it doesn't work in OkHTTP also. It's old socket problem, even in raw java sockets. Can it be effectively handled by using ping- pong mechanism? I think max delay of 2 sec should be allowed to detect socket disconnection? Can you show me the effective code to implement it?

TakahikoKawasaki commented 7 years ago

@sacOO7

// Get a WebSocket instance from somewhere.
WebSocket ws = ...;

// Send a ping per 2 seconds.
ws.setPingInterval(2 * 1000);

Please read "Send Ping/Pong Frames Periodically" in README.md.

stoefln commented 7 years ago

I noticed that onDisconnect is also not called in case the websocket server goes offline (switched off). Is this normal? Do I need to use pings? (A little bit more information on the purpose of Pings and Pongs would be cool!) Thanks for this awesome library!