Express.js – 如何将套接字传递给child_process

根据这个

https://nodejs.org/api/child_process.html#child_process_example_sending_a_socket_object

您可以轻松地将套接字连接传递给subprocess,以便它可以独立处理套接字事务。 你如何实现与客户端使用io.socket.js发送套接字请求的快速提供的req.socket对象相同的事情? 你不能这样做:

child.send("socket",req.socket") 

因为req.socket不是net.Socket的一个实例,所以它不能通过下面的检查

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

当请求是一个普通的HTTP请求(而不是在客户端上使用io.socket.js启动)时,req.Socket看起来只是net.Socket的一个实例。