Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何处理哪个子进程获得套接字。io套接字句柄_Javascript_Node.js_Socket.io - Fatal编程技术网

Javascript 如何处理哪个子进程获得套接字。io套接字句柄

Javascript 如何处理哪个子进程获得套接字。io套接字句柄,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,我正在使用Socket.io和集群。我需要做的是能够创建多个子进程,并在我认为合适的情况下向每个进程发送特定的[socket.io]套接字。当前(请参阅下面的代码),当我尝试将[socket.io]套接字作为句柄发送时,我收到一个异常 我下面的代码没有它(因为我正在尝试让最基本的示例工作),但我想做的是允许客户端连接并告诉它在收到某条消息时要执行什么过程。因此,客户机将发送一个包含一些数据的“init”消息,并基于该数据,我将[socket.io]套接字转发到特定进程。这是我最初的计划 我知道我

我正在使用Socket.io和集群。我需要做的是能够创建多个子进程,并在我认为合适的情况下向每个进程发送特定的[socket.io]套接字。当前(请参阅下面的代码),当我尝试将[socket.io]套接字作为句柄发送时,我收到一个异常

我下面的代码没有它(因为我正在尝试让最基本的示例工作),但我想做的是允许客户端连接并告诉它在收到某条消息时要执行什么过程。因此,客户机将发送一个包含一些数据的“init”消息,并基于该数据,我将[socket.io]套接字转发到特定进程。这是我最初的计划

我知道我可以让socket.io监听每个子进程,但是当客户端连接这些进程时,只有一个进程接收它

我的两个问题是:

  • 是否有方法将socket.io套接字发送到子进程
  • 如果没有,是否有其他方法来决定哪个进程获得句柄?(您是否可以在每个进程中使用不同的授权功能,如果一个进程不接受套接字,另一个进程可以?)


  • 您不应该以这种方式构造应用程序,因为它将不起作用。集群通常用于处理高负载,如果您想分散负载,可以使用集群和Redis存储

    除此之外,您不能将Socket.IO套接字对象发送给工作进程,因为它将无法通过此检查:

    if (handle instanceof net.Socket) {
      message.type = 'net.Socket';
    } else if (handle instanceof net.Server) {
      message.type = 'net.Server';
    } else if (handle instanceof process.binding('tcp_wrap').TCP ||
               handle instanceof process.binding('pipe_wrap').Pipe) {
      message.type = 'net.Native';
    } else if (handle instanceof dgram.Socket) {
      message.type = 'dgram.Socket';
    } else if (handle instanceof process.binding('udp_wrap').UDP) {
      message.type = 'dgram.Native';
    } else {
      throw new TypeError("This handle type can't be sent");
    }
    
    您无法真正选择将流程发送给哪个工作者,因此您应该考虑重新构造应用程序。即使有一个工作进程使用身份验证拒绝了套接字,客户端也必须保持随机重新连接以找到它想要的工作进程

    如果需要不同的套接字来接收不同的数据,那么应该查看文件室或名称空间。名称空间就是这样工作的:

    var nsp = io.of('/nsp');
    
    nsp.on('connection', function(socket) {
      // use socket here as you would with the global namespace
    });
    
    这就是房间的工作原理:

    io.sockets.on('connection', function(socket) {
      socket.join('room');
    });
    
    // emit to clients in that room
    io.sockets.in('room').emit('event_name', data);
    

    但您可以向子进程发送句柄。节点不尝试字符串化句柄(必须使用第二个参数,而不是第一个,请参阅链接)。还要注意的是,我之所以希望以这种方式构建它,是因为我希望在新的上下文和新的进程中运行不受信任的代码。我希望上下文尽可能直接地与套接字通信。我只是希望以前有人碰到过。啊,我没有做充分的研究。修改了答案。另外,为什么不从主进程运行所有套接字,并将代码而不是套接字发送到子进程?还要注意,我可以发送一个由
    net.createServer(函数(套接字){})创建的套接字。所以,我假设使用socket.io是不可能的。
    
    if (handle instanceof net.Socket) {
      message.type = 'net.Socket';
    } else if (handle instanceof net.Server) {
      message.type = 'net.Server';
    } else if (handle instanceof process.binding('tcp_wrap').TCP ||
               handle instanceof process.binding('pipe_wrap').Pipe) {
      message.type = 'net.Native';
    } else if (handle instanceof dgram.Socket) {
      message.type = 'dgram.Socket';
    } else if (handle instanceof process.binding('udp_wrap').UDP) {
      message.type = 'dgram.Native';
    } else {
      throw new TypeError("This handle type can't be sent");
    }
    
    var nsp = io.of('/nsp');
    
    nsp.on('connection', function(socket) {
      // use socket here as you would with the global namespace
    });
    
    io.sockets.on('connection', function(socket) {
      socket.join('room');
    });
    
    // emit to clients in that room
    io.sockets.in('room').emit('event_name', data);