0

I am pretty new with socket.io. I have a basic idea of how sockets work logically, but when it comes to the code I am a bit lost.


The Goal

I am creating a multiplayer game. I want users to be able to create a new game on the home page and use a specified username. Once the game is created, they can share the link to the game and others can join (when they hit that link, they are prompted to enter the user name they desire).

If another user goes to the webpage and wants to create a completely separate game, they can.

Each different game would be the same layout, and functionality.


Additional

I would prefer there to be one of those "click to copy url" to share the game and each game has a different url then the next, but is respective in that fact that the page layout is the same.


Again, I am almost completely at a loss as to how to do this. I would love if someone could actually give a coded example of the functionality with the logic. Everything I have found on google and the docs don't make sense in my head.

  • 1
    When the user wants to join a game, they would join the corresponding [room](https://socket.io/docs/v3/rooms/); you can use a router to manage URLs (e.g. [page.js](https://visionmedia.github.io/page.js/) is a simple but powerful clientside router, or you can route serverside in express); click to copy is written about extensively [here](https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript). However, comprehensive questions like these are not really answerable in StackOverflow. What _specifically_ doesn't make sense in your head? – Amadan Feb 09 '21 at 02:58
  • Sorry, just seeing this. I guess the idea of rooms itself. I can successfully create dynamic rooms on the front end, but i cant picture them in my head. If I have a user "start a new game" that will created a randomly generated code link, I cannot picture how it actually works. I want all the rooms to be generated based on a button click – Larry Schwall Feb 12 '21 at 01:44
  • `socket.io` rooms are serverside-only, and you can imagine them as lists of "names" of clients (well, IDs). "Enter a room" means the server puts the client's name on the room's list. "Leave a room" deletes the client from the list. "Broadcast to room" is send a message to each client on the list. One client can be on multiple lists at the same time. Since rooms only exist serverside, a "join room" button would have to send a message to the server, which would then add the client to the room; same for "create room". Client only ever talks to the server, so it doesn't have the concept of rooms. – Amadan Feb 12 '21 at 02:19
  • Try this: https://stackoverflow.com/a/72763264/11888809 – SWIK Jun 26 '22 at 16:22

0 Answers0