如何使用Nodejs net正确退出套接字

时间:2016-03-19 17:01:25

标签: 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);`

2 个答案:

答案 0 :(得分:1)

select * from (
    select *,
        dense_rank() over (partition by u.id order by c.id) rn,
        dense_rank() over (partition by c.id order by v.id) rn2
    from users u
    left join comments c on c.user_id = u.id
    left join votes v on v.comment_id = c.id
) t where rn < 4 and rn2 < 3

应该终止连接。

答案 1 :(得分:0)

如果您想在计算机上试用,请在此处拍摄摘录:

var net = require('net');

net.createServer(function(socket) {

    setInterval(function () {//Loop
     console.log("tick");
    }, 1000);

    socket.on('close', function () {
      socket.end();
      socket.destroy();
      console.log("socket closed");
    });

}).listen(1000);

socket.end()socket.destroy()并没有杀死net.createServer内的函数,程序一直在关闭套接字:S