-->

Web Socket: cannot detect client connection on int

2019-06-26 06:24发布

问题:

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.

回答1:

For TCP-based protocols (e.g. Websockets) generally what is done is heartbeat/ping packets are sent back and forth at the application layer so that each side can easily/quickly determine if the connection is gone for one reason or another.



回答2:

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