Socket.IO multiple pages for same user/socket

2019-05-29 02:42发布

问题:

I'm working on a multiplayer board game on Node.js/Socket.IO. The flow is the following :

  • user enters his name and is added to the lobby
  • user select a room and is added to it, then game starts when he is joined by another user.

This part is petty easy and I've done it before. However, I now need the user to be able to join multiple game rooms at the same time. pages are dynamically generated by express, and there's no issue opening many game pages, but I'm struggling with the socket implementation.

  • Can I use a single socket for multiple rooms (for the same user) or do I have to create a new socket per room?
  • I'd like the user to always be able to chat within the lobby while in game. How do I sort that out?

Thanks

回答1:

However, I now need the user to be able to join multiple game rooms at the same time. pages are dynamically generated by express, and there's no issue opening many game pages [...] Can I use a single socket for multiple rooms (for the same user) or do I have to create a new socket per room?

Pages open separately by the user do not share any context with each other. There are some hacky ways (such as a Flash LocalConnection), but you never should rely on these. Therefore, each page requires its own connection to your server.

I'd like the user to always be able to chat within the lobby while in game. How do I sort that out?

However you want. That implementation is up to you. If you are currently using the Socket.IO "rooms" feature, I suggest not using it so you have maximum flexibility in your implementation.