简体   繁体   English

node.js将socket.io socket传递给forked进程

[英]node.js pass socket.io socket to forked process

I'm wondering if there is any way to pass a socket.io socket to a forked process in node.js, or reconstruct a socket.io socket in the forked process? 我想知道是否有任何方法可以将socket.io套接字传递给node.js中的分叉进程,或者在分叉进程中重构socket.io套接字?

child = require("child_process").fork("worker.js")
io.sockets.on("connection", function(socket){
    child.send("socket", socket); //this definitely fails now
})

Solution : Use Redis 解决方案:使用Redis

Im sure you will be happy to know about the existance of socket.io-redis 我相信你会很高兴知道socket.io-redis的存在

With it you will be able to send/receive events (share clients) between multiple instances of your code (processes or servers). 有了它,您将能够在代码的多个实例(进程或服务器)之间发送/接收事件(共享客户端)。 Read more : socket.io-redis (Github Repo.) 阅读更多:socket.io-redis(Github Repo。)

With this you can scale : 有了这个你可以扩展:

  1. vertically (increase the number of threads per machine) 垂直 (增加每台机器的线程数)
  2. horizontally (deploy more "masters" accross more machines) 水平 (在更多机器上部署更多“主人”)

There is an example app at github github上有一个示例应用程序

This is a very simple Node.JS client/server application that shows how to setup Express.js, Socket.IO 1.x and socket.io-redis together. 这是一个非常简单的Node.JS客户端/服务器应用程序,它显示了如何将Express.js,Socket.IO 1.x和socket.io-redis一起设置。

This is very useful as a starting point to understand how to scale Socket.IO RealTime Applications that need multiple server processes. 这非常有用,可以作为了解如何扩展需要多个服务器进程的Socket.IO RealTime应用程序的起点。 Redis is used as a storage point (adapter) in order to run multiple socket.io instances in different processes or servers that can all broadcast and emit events to and from each other. Redis用作存储点(适配器),以便在不同进程或服务器中运行多个socket.io实例,这些进程或服务器都可以相互广播和发送事件。

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

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