Web Socket:无法在 Internet 断开连接时检测到客户端连接 [英] Web Socket: cannot detect client connection on internet disconnect

查看:69
本文介绍了Web Socket:无法在 Internet 断开连接时检测到客户端连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果客户端使用网络套接字关闭互联网,我想检测客户端连接.我的代码是:

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');  
  });
})

我使用了那个代码 (webSocket.on('disconnect',function()) 但没有奏效.

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

推荐答案

WebSocket 基于 TCP,TCP 使用 FIN 包来关闭连接.在互联网连接突然中断的情况下,WebSocket 服务器和手机都不知道已经死的 TCP 连接,因为没有发送 FIN 数据包.

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.

为了解决这个问题,TCP 有一个机制叫做keepalive.

To address the issue, TCP has a mechanism called keepalive.

我为解决该问题所做的是调整 Linux 内核中的 TCP keepalive 设置,并调用 ws._socket.setKeepAlive(true).

What I did to solve the issue is adjusting the TCP keepalive settings in Linux kernel, and invoke ws._socket.setKeepAlive(true).

参考:https://github.com/websockets/ws/issues/353

这篇关于Web Socket:无法在 Internet 断开连接时检测到客户端连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆