Multiplayer tutorial 2: chat room

25

Index

Features on these Courses

Stats

42,586 visits, 162,877 views

Tools

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.

Published on 14 Mar, 2014. Last updated 29 Apr, 2024

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.

  • 14 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hi.i love construct.but I have a problem .

    only when peers and host,s adsl are same, it works can you help me.

    oh. and sorry.Im not english and its hard to write it.but I cant do any thing else.

    sorry. and please answer my question.

    thanks for your awesome program:construct.

  • Aren't chatrooms not allowed?

      • [-] [+]
      • 1
      • Ashley's avatar
      • Ashley
      • Construct Team Founder
      • 1 points
      • (1 child)

      You can make whatever content you like with Construct. However due to moderation problems, chat rooms are not allowed in the Construct Arcade. You can however publish chat rooms anywhere else.

  • How did you get the tab order to work on this? I see that I can tab from NameTextBox (UID 2) to RoomNameTextBox (UID 13) to JoinButton (UID 3). I can't find this anywhere and all my google searches say it can't be done, that you can only tab in order of UID, yet you did it in this project. Please help!!!

  • Hi, I tried to export the chat template to Android Debug apk and installed on my phone and try to host a room. It successfully connected and created the room as host. But when I try to connect another peer using my laptop browser it says connected as peer but will be kicked eventually. Am I missing something? Can I use multiplayer on Android device? Is the multiplayer plugin for browsers only?

  • sorry, where i can find the link to download the archive?

    • "To get going, open the Example: Multiplayer - chat room example that comes with Construct 2. This can be found in the Select template or example dialog that opens when pressing File - New."

  • Load more comments (4 replies)