node.js 在 Socket.io 客户端获取连接状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16518153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:29:12  来源:igfitidea点击:

Get connection status on Socket.io client

node.jssocket.io

提问by franzlorenzon

I'm using Socket.io, and I'd like to know the status of connection to the server from the client-side.

我正在使用Socket.io,我想知道从客户端到服务器的连接状态。

Something like this:

像这样的东西:

socket.status // return true if connected, false otherwise

I need this information to give a visual feedback to the user if the connection has dropped or it has disconnected for any reason.

如果连接断开或由于任何原因断开连接,我需要此信息向用户提供视觉反馈。

回答by robertklep

You can check the socket.connectedproperty:

您可以检查socket.connected属性:

var socket = io.connect();
console.log('check 1', socket.connected);
socket.on('connect', function() {
  console.log('check 2', socket.connected);
});

It's updated dynamically, if the connection is lost it'll be set to falseuntil the client picks up the connection again. So easy to check for with setIntervalor something like that.

它是动态更新的,如果连接丢失,它将被设置为false直到客户端再次连接。很容易检查setInterval或类似的东西。

Another solution would be to catch disconnectevents and track the status yourself.

另一种解决方案是捕获disconnect事件并自己跟踪状态。

回答by Ansari Abdullah

You can check whether the connection was lost or not by using this function:-

您可以使用此功能检查连接是否丢失:-

var socket = io( /**connection**/ );
socket.on('disconnect', function(){
//Your Code Here
});

Hope it will help you.

希望它会帮助你。

回答by Ilan

Track the state of the connection yourself. With a boolean. Set it to falseat declaration. Use the various events (connect, disconnect, reconnect, etc.) to reassign the current boolean value. Note: Using undocumented API features (e.g., socket.connected), is not a good idea; the feature could get removed in a subsequent version without the removal being mentioned.

自己跟踪连接状态。带有布尔值。将其设置为falseat 声明。使用各种事件(连接、断开连接、重新连接等)重新分配当前布尔值。注意:使用未记录的 API 功能(例如socket.connected)不是一个好主意;该功能可能会在后续版本中删除,而不会提及删除。

回答by Na Nonthasen

These days, socket.on('connect', ...) is not working for me. I use the below code to check at 1st connecting.

这些天,socket.on('connect', ...) 对我不起作用。我使用以下代码在第一次连接时进行检查。

if (socket.connected)
  console.log('socket.io is connected.')

and use this code when reconnected.

并在重新连接时使用此代码。

socket.on('reconnect', ()=>{
  //Your Code Here
});

回答by Qiulang

@robertklep's answer to check socket.connected is correct except for reconnect event, https://socket.io/docs/client-api/#event-reconnectAs the document said it is "Fired upon a successful reconnection." but when you check socket.connectedthen it is false.

@robertklep 检查 socket.connected 的答案是正确的,除了重新连接事件,https://socket.io/docs/client-api/#event-reconnect 正如文档所说,它是“成功重新连接时触发”。但是当您检查时,socket.connected它是错误的。

Not sure it is a bug or intentional.

不确定这是一个错误还是故意的。