简体   繁体   English

使用socket.io连接和断开连接的复杂性是什么?

[英]What's the complexity of connecting and disconnecting with socket.io?

I'm running a node.js server. 我正在运行一个node.js服务器。 On my website I use different URLs for different pages. 在我的网站上,我为不同的页面使用了不同的URL。 For instance: 例如:

mydomain.com/           -- Index
mydomain.com/register   -- Register
mydomain.com/profile    -- Profile

I am using socket.io to send chat messages and notifications to the client. 我正在使用socket.io将聊天消息和通知发送到客户端。 However, whenever the user switches page or performs a POST-request the socket connection is disconnected and then reconnected. 但是,每当用户切换页面或执行POST请求时,套接字连接都会断开,然后重新连接。

I'm wondering what the complexity of socket.io's connect/disconnect functions are and wether it is durable that all clients reconnect their sockets each time they perform an action on my website? 我想知道socket.io的连接/断开功能的复杂性是什么,是否持久性使得所有客户端每次在我的网站上执行操作时都重新连接其套接字? I've looked at the documentation for socket.io without finding the answer. 我看了socket.io的文档却找不到答案。

That's what I have found, but I would guess it would really depend on your code. 这就是我所发现的,但是我想这真的取决于您的代码。 If each page connects to the channel then the connection will be reestablished. 如果每个页面都连接到该频道,则将重新建立连接。 I am also in the habit of storing messages on the server side to reestablish state - something to this effect: 我还习惯于在服务器端存储消息以重新建立状态-达到这种效果:

const events = []
const onSomeEvent = function(myEvent) {
      events.push(myEvent)
      socket.emit("onSomeEvent", myEvent)
}
socket.on("connect", function(){
    events.forEach(function(myEvent){
       socket.emit("onSomeEvent", myEvent)
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM