Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Socket: cannot detect client connection on internet disconnect

I want to detect the client connection if client turn off the internet using web socket. My code is:

 //Include util library
    var util = require('util');
    // Include underscore library
    var _ = require('underscore')._;
    //For websocket
var webSocketServer = new (require('ws')).Server({port: (process.env.PORT || 5000)}),
webSockets = {} // userID: webSocket

// CONNECT /:userID
// wscat -c ws://localhost:5000/1
webSocketServer.on('connection', function (webSocket) 
{
  var userID = webSocket.upgradeReq.url.substr(1);
  //console.log('User_id is ',userID);
  webSockets[userID] = webSocket
                   util.log('User_id: [' + userID + '] enter in connected users list of [ ' + Object.getOwnPropertyNames(webSockets)+' ]')
                   // Call function which check id exist in letswalkee DB table
                   check_userid(userID);


                   // Send msg like: [fromUserID, text]    [1, "Hello, World!"]
webSocket.on('message', function(message) {
util.log('Received from [' + userID + ']: ' + message)
var messageArray = JSON.parse(message)
                                    var toUserWebSocket = webSockets[messageArray[0]]
                                    if (toUserWebSocket) {
                                    util.log('Sent to [' + messageArray[0] + ']: ' + JSON.stringify(messageArray))
                                    messageArray[0] = userID
                                    toUserWebSocket.send(JSON.stringify(messageArray))
                                    }
                                    })

                   webSocket.on('close', function () 
                   {
                      delete webSockets[userID]
                      util.log('User_id Deleted from connected users: [ ' + userID+' ]');

                    })
webSocket.on('disconnect',function()
  {
   console.log('hello i am disconnected');  
  });
})

I used that code (webSocket.on('disconnect',function()) but did not worked.

like image 706
Waleed Amjad Avatar asked Oct 20 '22 03:10

Waleed Amjad


1 Answers

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 unaware 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 ws._socket.setKeepAlive(true).

Reference: https://github.com/websockets/ws/issues/353

like image 119
Waleed Amjad Avatar answered Oct 23 '22 05:10

Waleed Amjad