简体   繁体   English

node.js - 共享进程之间的套接字

[英]node.js - share sockets between processes

I´ve read that it´s possible to share sockets between processes. 我读过可以在进程之间共享套接字。 Is this also possible in Node.js? 这在Node.js中也可以吗?

I saw the cluster api in node.js, but that´s not what I´m looking for. 我在node.js中看到了集群api,但这不是我想要的。 I want to be able to accept a connection in one process, maybe send & read a bit, and after a while pass this socket to another fully independent node.js process. 我希望能够在一个进程中接受连接,可能发送和读取一段时间,并在一段时间后将此套接字传递给另一个完全独立的node.js进程。

I could already do this with piping, but I don´t want to do this, since it´s not as fast as directly reading/writing to the socket itself. 我已经可以使用管道来做到这一点,但我不想这样做,因为它没有直接读/写套接字本身那么快。

Any ideas? 有任何想法吗?

Update I found the following entry in the node.js documentation: 更新我在node.js文档中找到以下条目:

new net.Socket([options]) #
Construct a new socket object.

options is an object with the following defaults:

{ fd: null
  type: null
  allowHalfOpen: false
}
fd allows you to specify the existing file descriptor of socket. type specified underlying protocol. It can be 'tcp4', 'tcp6', or 'unix'. About allowHalfOpen, refer to createServer() and 'end' event.

I think it would be possible to set the "fd" property to the filedescriptor of the socket and then open the socket with that. 我认为可以将“fd”属性设置为套接字的filedescriptor,然后用它打开套接字。 But... How can I get the filedescriptor of the socket and pass it to the process that needs it? 但是......如何获取套接字的filedescriptor并将其传递给需要它的进程?

Thanks for any help! 谢谢你的帮助!

This is not possible at the moment, but I've added it as a feature request to the node issues page. 目前这是不可能的,但我已将其作为功能请求添加到节点问题页面。

Update In the mean time, I've written a module for this. 更新同时,我为此编写了一个模块。 You can find it here: https://github.com/VanCoding/node-ancillary 你可以在这里找到它: https//github.com/VanCoding/node-ancillary

You probably want to take a look at hook.io 你可能想看看hook.io

hook.io is a distributed EventEmitter built on node.js. hook.io是一个基于node.js构建的分布式EventEmitter。 In addition to providing a minimalistic event framework, hook.io also provides a rich network of hook libraries for managing all sorts of input and output. 除了提供简约事件框架外,hook.io还提供了丰富的钩子库网络,用于管理各种输入和输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM