node.js - 如何使用 Nodejs net 正确退出套接字

标签 node.js sockets

我在尝试终止nodejs net中套接字函数内的所有进程时遇到问题:套接字断开连接后循环继续运行。有没有办法杀死套接字调用的所有进程?

net.createServer(function (socket) {

  socket.on('end', function () {
    //How kill all processes of the current socket ???
  });
  socket.on('error', function () {
    //How kill all processes of the current socket ???
  });

  i=0;
  setInterval(function () {//Waiting other peer for 5sec
    i++;
    console.log("loop");
    if(i>=10){
      clearInterval(this);
    }
  }, 1000);

}).listen(1000);

最佳答案

socket.disconect(0);

应该终止连接。

关于node.js - 如何使用 Nodejs net 正确退出套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104549/

相关文章:

node.js - NodeJS/Typescript 导出/导入的正确解释?

node.js - 如何在nodejs中使用pdfkit将png格式保存为pdf格式[包含代码]

sockets - 关闭读边后是否读套接字?

.net - VB.NET中可靠的网络重新连接循环

c# - 没有证书的 SslStream

sockets - 无法捕获 SocketException

android - 处理 FCM 设备组的刷新 token

node.js - 将 htaccess 添加到 Ghost 项目

windows - 专为 Unix 设计的 Node.js 模块?

c - 接受函数不等待连接(不阻塞)