简体   繁体   English

Node.js集群将连接传递给特定工作程序

[英]Node.js cluster passing connection to specific worker

I have my multiplayer game based on socket.io library and i want to scale it on multiple threads. 我有基于socket.io库的多人游戏,我想在多个线程上进行缩放。 I've learned that node.js have cluster module to handle multithreading. 我了解到,node.js具有用于处理多线程的集群模块。 My game world is made of multiple zones and I want each zone to be handled in it's own proccess. 我的游戏世界由多个区域组成,我希望每个区域都在自己的过程中进行处理。 I need a method to pass user's socket to specific worker, so user could move from zone to zone. 我需要一种将用户的套接字传递给特定工作人员的方法,以便用户可以从一个区域移动到另一个区域。 I've read in node.js docs that cluster module uses round-robin method to pick worker for every connection. 我已经阅读了node.js文档,该集群模块使用轮询方法为每个连接选择工作程序。 Maybe there is any workaround to manipulate connections? 也许有任何解决方法来操纵连接?

I'm not entirely sure that clustering is the right solution for you. 我不完全确定群集是否适合您。 The main advantage of clustering is that it does automatic balancing of the incoming connections among the different processes. 群集的主要优点是,它可以自动平衡不同进程之间的传入连接。 But, you don't want to use that capability - you want to manually assign each connection to the process that is handling a given zone. 但是,您不想使用该功能-您希望将每个连接手动分配给正在处理给定区域的进程。

As such, I'm thinking that you should just start up the number of processes you want, assign them each a port that is specific to that zone and then have the client send an Ajax request to the master asking what port should they do a socket.io connection to for their zone. 因此,我认为您应该启动所需的进程数,为每个进程分配一个特定于该区域的端口,然后让客户端向主服务器发送一个Ajax请求,询问它们应该执行哪个端口。用于其区域的socket.io连接。 The client gets that ajax response and then connects directly to the appropriate server for their zone. 客户端获得该ajax响应,然后直接连接到其区域的适当服务器。 All your socket.io servers would need to allow cross-origin requests from the desired master page domain/port. 您所有的socket.io服务器都需要允许来自所需母版页域/端口的跨域请求。

Connecting to the server for a zone could either be done via port as described above or it could be done via a separate hostname such as zone1.mygameserver.com and then use something like nginx to route all the different hosts to specific ports on your server box. 可以如上所述通过端口完成连接到区域的服务器,也可以通过单独的主机名(例如zone1.mygameserver.com完成连接,然后使用诸如nginx之类的东西将所有不同的主机路由到服务器上的特定端口框。

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

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