Multiplayer tutorial 2: chat room

1

Index

Taggé

Statistiques

5,255 visites, 8,816 vues

Outils

Partager

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Signalling group

This group deals with the signalling server. We must connect to signalling to find out who else is in the room, be able to connect to them, and find out if we were made the host. First of all we must connect to the signalling server, which is the official server hosted by Scirra at wss://multiplayer.scirra.com. (Each step is logged to the chat log as well so the user can see the progress as it happens.)

A moment later assuming all has gone well On signalling connected triggers. However the signalling server won't allow us to join any games until we log in with our username (called the alias by the signalling server). So upon connection, we also request to log in with the username from the Login screen.

Then assuming our login request was successful, On signalling logged in triggers. We are now ready to join a game. Note that if the requested alias is already in use on the signalling server, it may simply assign you a new alias. For example if two people login as "Joe", the second person actually gets the alias "Joe2". To avoid confusion it's a good idea to display back to the user their assigned alias (from the Multiplayer.MyAlias expression) so they see what their own name really is. Now that we're logged in we can also request to join the room (using our global constants for the game, instance and room names).

Upon joining the room, we find out for the first time if we have become the host or not. The signalling server will make the first person in to a room the host. The actual game data is transferred via that person's connection (they act like the server for the game). We test if we became the host using the Is host condition in a sub-event to the On signalling joined room trigger. Since both the host (acting as the server) and peers (acting as clients to the server) run the game from the same single project, it's sensible to organise events in to two different Host and Peer groups. Each are initially deactivated, but upon joining the room we activate the appropriate one. This means we have a convenient place to put events that should only apply to the host or to peers. We also add our own name to the peer list, since we won't receive a "peer joined" notification for ourselves.

The last three events in this group are basically just for handling errors. In the vast majority of cases all the above events should work fine. However if we leave the room, get disconnected from the signalling server, or encounter an error on the signalling server, we add a message to the chat log so the user can see what happened.

This group has taken us through connecting, joining the room on the signalling server, and activating the appropriate group for being the host or a peer. Once joined, the game data runs via the host only. The rest of the events deal with the chat room itself. These are another three Common, Peer and Host groups under the Chat parent group.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!