Socket.io:如何处理关闭连接?

时间:2022-09-22 19:43:56

I'm trying to understand which are the "physical" limits of my application.

我试图了解哪些是我的应用程序的“物理”限制。

On the client side:

在客户端:

var socket = io.connect("http://127.0.0.1:6701");
socket.on('connect', function() {
  socket.disconnect();
});
socket.on('disconnect', function() {
  socket.socket.reconnect();
});

On the server side:

在服务器端:

var io = require('socket.io').listen(6701);

io.sockets.on('connection', function(socket) {

  socket.on('disconnect', function(reason) {
    var o = Object.keys(socket.manager.open).length
      , c = Object.keys(socket.manager.closed).length
      , cA = Object.keys(socket.manager.closedA).length
      , h = Object.keys(socket.manager.handshaken).length

      console.log("open: " + o)
      console.log("closed: " + c)
      console.log("handshaken: " + h)
      console.log("closedA: " + cA)
      console.log("---");
   });
});

When in OSX I hit the file limit (256) the statistics are the following

在OSX中我达到文件限制(256)时,统计数据如下

open: 471
closed: 235
handshaken: 471
closedA: 235

What is puzzling me is:

令我困惑的是:

  • if I forcefully close the connection (this is what I would like to do with the client disconnect(), why I'm still using a file handle (so I hit the file limit) edit: adding a delay seems that the server can keep breath and never reaches the file limit)?
  • 如果我强行关闭连接(这是我想用客户端disconnect(),为什么我仍然使用文件句柄(所以我达到文件限制)编辑:添加延迟似乎服务器可以保持呼吸,永远不会达到文件限制)?
  • Is there a way to completely close the socket so I could be pretty sure the file limit is rarely reached (I know that I can push it to 65K)?
  • 有没有办法完全关闭套接字,所以我可以非常肯定很少达到文件限制(我知道我可以把它推到65K)?
  • Is there a way to know that I'm reaching the file limit, so that I can stop accepting connection?
  • 有没有办法知道我达到了文件限制,以便我可以停止接受连接?

Thank you

谢谢

1 个解决方案

#1


1  

It's better if client sends a "terminate" command to server which then closes the connection, than the other way around.

如果客户端向服务器发送“终止”命令然后关闭连接,那么最好是相反。

Server will always wait until a timeout before giving up on a connection. Even if that timeout is small, with loads of connections coming in, it will overload it. That's why, for example, is always good to disable keep-alive on app servers.

在放弃连接之前,服务器将一直等到超时。即使超时很小,加载了大量连接,它也会超载。例如,这就是为什么在app服务器上禁用keep-alive总是好的。

Delay helps because server has time to close the connection before opening a new one.

延迟有帮助,因为服务器有时间在打开新连接之前关闭连接。

#1


1  

It's better if client sends a "terminate" command to server which then closes the connection, than the other way around.

如果客户端向服务器发送“终止”命令然后关闭连接,那么最好是相反。

Server will always wait until a timeout before giving up on a connection. Even if that timeout is small, with loads of connections coming in, it will overload it. That's why, for example, is always good to disable keep-alive on app servers.

在放弃连接之前,服务器将一直等到超时。即使超时很小,加载了大量连接,它也会超载。例如,这就是为什么在app服务器上禁用keep-alive总是好的。

Delay helps because server has time to close the connection before opening a new one.

延迟有帮助,因为服务器有时间在打开新连接之前关闭连接。