简体   繁体   中英

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. I've learned that node.js have cluster module to handle multithreading. 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. 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. The client gets that ajax response and then connects directly to the appropriate server for their zone. All your socket.io servers would need to allow cross-origin requests from the desired master page domain/port.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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