Socket.IO multiple pages for same user/socket

穿精又带淫゛_ 提交于 2020-01-14 02:53:26

问题


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.



来源:https://stackoverflow.com/questions/20504642/socket-io-multiple-pages-for-same-user-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!