nv-websocket-client
nv-websocket-client copied to clipboard
WebSocket is disconnecting after 2 mins
Hi,
- Launching the app the websocket connection was successful.
- keep app idle for 2 min webscoket is disconnected automatically and giving below error WebSocketFrame(FIN=1,RSV1=0,RSV2=0,RSV3=0,Opcode=CLOSE,Length=42,CloseCode=1002,Reason="No more WebSocket frame from the server.")
- After socket is disconnected, how to connect socket again without killing the app
Websocket client connection logic:
try {
if (webSocket == null) {
webSocket = new WebSocketFactory().createSocket(endPoint);
webSocket.addHeader("Authorization", idToken);
webSocket.setPingInterval(600000);
webSocket.addListener(webSocketAdapter);
Log.i(TAG, "ws.connectAsynchronously () is null =" + webSocket.toString());
} else {
webSocket = webSocket.recreate();
Log.i(TAG, "ws.connectAsynchronously () is not null =" + webSocket.toString());
}
webSocket.connectAsynchronously();
} catch (IOException e) {
e.printStackTrace();
}
Need help to resolve this.
Same error. I'm catching the onDisconnected
event:
@Override
public void onDisconnected(WebSocket websocket,
WebSocketFrame serverCloseFrame,
WebSocketFrame clientCloseFrame,
boolean closedByServer) {
reconnect();
}
And the parameter closedByServer
is always false. I've not set any ping interval. What can be the reason?
This is usually a server-side problem; e.g. if your webserver is behind an nginx reverse proxy (e.g. kubernetes ingress), the default parameters make it disconnect in 2 minutes.
Hi,
I also get the onDisconnected
notification with the reason: CloseCode=1002,Reason="No more WebSocket frame from the server
Stacktrace:
0527-122505.965-W-(WebSocketSession) - Disconnected. websocket=com.neovisionaries.ws.client.WebSocket@7d891dd9, serverCloseFrame=nullclientCloseFrame=WebSocketFrame(FIN=1,RSV1=0,RSV2=0,RSV3=0,Opcode=CLOSE,Length=42,CloseCode=1002,Reason="No more WebSocket frame from the server.") java.lang.Exception: Stack trace at java.lang.Thread.dumpStack(Thread.java:1333) at icsii.dsreuters.ws.WebSocketSession.onDisconnected(WebSocketSession.java:245) at com.neovisionaries.ws.client.ListenerManager.callOnDisconnected(ListenerManager.java:224) at com.neovisionaries.ws.client.WebSocket.finish(WebSocket.java:3758) at com.neovisionaries.ws.client.WebSocket.onThreadsFinished(WebSocket.java:3724) at com.neovisionaries.ws.client.WebSocket.onWritingThreadFinished(WebSocket.java:3713) at com.neovisionaries.ws.client.WritingThread.notifyFinished(WritingThread.java:539) at com.neovisionaries.ws.client.WritingThread.runMain(WritingThread.java:78) at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
In my case, I receive the same data from the same source with different WebSocket sessions (on different hosts). But some of them sometimes gives above onDisconnected
case.
As seen below, Session 1 does not get any data after 13:25:17 and onDisconnected
is called after 79 seconds. But Session 2 gets data and has no problem.
Session 1 (on host 1)
0527-132517.718-I-(WebSocketSession) - WSOCK: {instr='TRY=', ASK=8.3706, BID=8.3665}
0527-132517.748-I-(WebSocketSession) - WSOCK: {instr='XAU=', ASK=1885.88, BID=1885.11}
0527-132636.689-W-(WebSocketSession) - WSOCK: Disconnected. closedByServer=false
0527-132636.690-W-(WebSocketSession) - WSOCK: Disconnected. websocket=com.neovisionaries.ws.client.WebSocket@60137160, serverCloseFrame=nullclientCloseFrame=WebSocketFrame(FIN=1,RSV1=0,RSV2=0,RSV3=0,Opcode=CLOSE,Length=42,CloseCode=1002,Reason="No more WebSocket frame from the server.")
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1333)
at icsii.dsreuters.ws.WebSocketSession.onDisconnected(WebSocketSession.java:245)
at com.neovisionaries.ws.client.ListenerManager.callOnDisconnected(ListenerManager.java:224)
at com.neovisionaries.ws.client.WebSocket.finish(WebSocket.java:3758)
at com.neovisionaries.ws.client.WebSocket.onThreadsFinished(WebSocket.java:3724)
at com.neovisionaries.ws.client.WebSocket.onWritingThreadFinished(WebSocket.java:3713)
at com.neovisionaries.ws.client.WritingThread.notifyFinished(WritingThread.java:539)
at com.neovisionaries.ws.client.WritingThread.runMain(WritingThread.java:78)
at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
Session 2 (on host 2)
0527-132517.280-I-(WebSocketSession) - WSOCK: {instr='XAU=', ASK=1885.94, BID=1884.93}
0527-132517.640-I-(WebSocketSession) - WSOCK: {instr='XAU=', ASK=1886.06, BID=1885.55}
0527-132517.641-I-(WebSocketSession) - WSOCK: {instr='TRY=', ASK=8.3714, BID=8.3663}
0527-132517.732-I-(WebSocketSession) - WSOCK: {instr='TRY=', ASK=8.3706, BID=8.3665}
0527-132517.760-I-(WebSocketSession) - WSOCK: {instr='XAU=', ASK=1885.88, BID=1885.11}
0527-132518.701-I-(WebSocketSession) - WSOCK: {instr='XAU=', ASK=1886.07, BID=1885.56}
0527-132518.701-I-(WebSocketSession) - WSOCK: {instr='TRY=', ASK=8.3716, BID=8.3661}
0527-132518.810-I-(WebSocketSession) - WSOCK: {instr='XAU=', ASK=1885.9, BID=1884.8}
0527-132518.971-I-(WebSocketSession) - WSOCK: {instr='XAU=', ASK=1885.98, BID=1885.21}
Websocket client connection logic is:
webSocket = websocketFactory
.createSocket(url)
.addProtocol("tr_json2")
.addListener(this)
.addExtension(WebSocketExtension.PERMESSAGE_DEFLATE);
You could try sending pings and enable pongs on your server.
This fixed the issue for me.
Use the Websocket#setPingInterval
method for this.
webSocket = websocketFactory
.createSocket(url)
.addProtocol("tr_json2")
.addListener(this).setPingInterval(pingDuration)
.addExtension(WebSocketExtension.PERMESSAGE_DEFLATE);