-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to check client is connected in web socket? #353
Comments
I have the same problem. The weird thing is that even the device is disconnected, when the server tries to send some message to the device, there is no error and the server still has no idea about the dead connection. This is quite annoying, because it could make my data inconsistent. BTW, I am using https://github.com/mkuklis/phonegap-websocket on my android 4.0.4 device. |
👍 |
@zwz But I want web socket for ios to remove the dead connection? |
@waleedbutt It seems the server will remove them after some time. |
I had the same issue and after some Googling, here is result: WebSocket is based on TCP and TCP uses FIN packet to close the connection. In the case of sudden loss of Internet connection, both the WebSocket server and the phone are unware of the already dead TCP connection because no FIN packet was sent. To address the issue, TCP has a mechanism called keepalive. What I did to solve the issue is adjusting the TCP keepalive settings in Linux kernel, and invoke I am going to propose a new public method |
Hi humingchun |
Hi waleedbutt, for the detailed introduction for TCP keepalive machanism, you could refer to the link I posted before. And after the keepalive detects the failure of the connection, TCP layer will raise a For example, you want to detect a dead TCP connection within 10 seconds, you can set following kenel params:
It will add a little more extra network traffic, but you can choose to enable keep alive to specific TCP connection by invoking |
this is interesting - but let me clarify something: what when i am on the road with the mobile, having an open websocket connection to the server for app X but loose connection for few seconds during a cell handover? will the above trick resume the same connection or create a new one? |
binarykitchen,it depends on your client side implementation.As i tested on android, immediately after re-connection it will close the current session and generate a new one. |
why can't we re-use the session when re-connecting? @Spentas |
simply cause your ip address may be changed upon reconnection and the old session is bound to an invalid ip address |
hmmm, are websocket sessions really bound to an ip address instead of a token?? |
binarykitchen, websocket is basically a tcp/ip connection, there is no reconnection option. What we do in our android app, we use telephone manager to subscribe on cell change event. If it triggers, we just do the reconnect manually |
sure i understand that. reason i am asking all that is because my www.videomail.io app is having issues on androids when you are moving from one cell to another. when reconnected, new streams are instantiated instead of the existing one which makes it very difficult to send data to the same destination on the server side. |
How to implent the fix @humingchun . Where do i set ws._socket.setKeepAlive? |
Hi, I am using this library in Node.js - https://www.npmjs.com/package/express-ws But I am not finding any way of running this code-
Can anyone please help? |
Hi,
You can found answer here for WS level implementation of disconnect detection. |
A possible way is to use something like this: https://github.com/websockets/ws#how-to-detect-and-close-broken-connections. |
@lpinca: that's only for verifying as a server, that the client is still connected. How about reconnecting, as a client, when the connection no longer sends data for a while? |
Add a timer on the client that terminates the connection when it expires, reset the timer every time a ping (or any other data) is received from the server. |
@lpinca: that's what I did in this sample, but it's not that straightforward to reconnect. Not even reconnecting-websocket manages to. |
Is there any close event that the websockets emit which we can listen to whenever a connection breaks. eg: or can I add the same logic whenever a new connection opens? |
setTimeout(function() { |
My question is that I am using web socket for my iphone native app so if user is disconnected the internet then web socket cannot get the state of client that it is close or disconnect. my code is here
var webSocketServer = new (require('ws')).Server({port: (process.env.PORT || 5000)}),
webSockets = {} // userID: webSocket
//Delete userID from connected list of userID
webSocket.on('close', function ()
{
delete webSockets[userID]
util.log('User_id: [' + userID+'] deleted from Connected users list: ['+ Object.getOwnPropertyNames(webSockets)+' ]');
})
but if i use that code it did not work.
// if client is disconnect with internet
webSocket.on('disconnect',function()
{
console.log('hello i am disconnected');
});
The text was updated successfully, but these errors were encountered: