Socket.io:如何处理关闭的连接? [英] Socket.io: How to handle closing connections?

查看:1901
本文介绍了Socket.io:如何处理关闭的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解哪些是应用程序的物理"限制.

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

在客户端:

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

在服务器端:

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("---");
   });
});

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

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

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

让我感到困惑的是:

  • 如果我强制关闭连接(这是我想对客户端disconnect()进行的操作,为什么我仍在使用文件句柄(所以我达到了文件限制)添加延迟似乎服务器可以保持呼吸,并且永远不会达到文件限制)?
  • 是否有一种方法可以完全关闭套接字,以便可以确定很少达到文件限制(我知道可以将其推到65K)?
  • 是否有办法知道我已达到文件限制,以便我可以停止接受连接?
  • 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)?
  • 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)?
  • Is there a way to know that I'm reaching the file limit, so that I can stop accepting connection?

谢谢

推荐答案

如果客户端向服务器发送一个终止"命令,然后关闭连接,最好比其他方法更好.

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.

延迟之所以有用,是因为服务器在打开新连接之前有时间关闭连接.

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

这篇关于Socket.io:如何处理关闭的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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