Multiplayer tutorial 2: chat room

Favourite 82 favourites
Tutorial written by AshleyOriginally published on 14th, March 2014 - 3 revisions

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.)

https://www.scirra.com/images/articles/mp2-ev4.png


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.

https://www.scirra.com/images/articles/mp2-ev5.png


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).

https://www.scirra.com/images/articles/mp2-ev6.png


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.

https://www.scirra.com/images/articles/mp2-ev7.png


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.

https://www.scirra.com/images/articles/mp2-ev10.png


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.

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

5
Tedg 9,884 rep

Wow this is fantastic .!

Friday, March 14, 2014 at 3:36:23 PM
2
GodSpear 2,741 rep

Спасибо хороший

Friday, March 14, 2014 at 4:00:21 PM
6
RamPackWobble 30.8k rep

Thanks for this - its going to need a few reads before it gets through my thick skull, but I'm sure it will.

Friday, March 14, 2014 at 4:40:00 PM
4
iceangel 32.5k rep

chat room ... interesting!!!

Friday, March 14, 2014 at 4:53:08 PM
2
jardelbr 4,857 rep

PERFECT! Chat room is so much important for MP games!

Friday, March 14, 2014 at 6:06:41 PM
3
AllanR 17.6k rep

VERY Exciting stuff!!! Can't wait to get started!

Friday, March 14, 2014 at 7:04:49 PM
1
exertia 2,625 rep

@Ashley - say my game can only work well with 2 players i.e. one host and one peer - is there anyway I can limit my multiplayer sessions to 2 players only?

Friday, March 14, 2014 at 8:40:35 PM
3
sggs 3,555 rep

Thanks for laying the foundation for all my new games.

Friday, March 14, 2014 at 9:40:55 PM
2
edisone 18.3k rep

very very interesting...

Saturday, March 15, 2014 at 4:01:29 PM
2
Mohorelien 3,220 rep

We'll translate these tutorials soon.

Sunday, March 16, 2014 at 12:59:32 PM
1
Matiu 921 rep

So awesome:D One question however I am trying to get the chat example to work on a server with https protocol. I get this message...

Connecting to server...
Signalling error: SyntaxError: Failed to construct 'WebSocket': The URL's scheme must be either 'ws' or 'wss'. 'https' is not allowed.

Anyone know if it is possible to run multiplayer over a https connection?

Monday, March 17, 2014 at 9:24:17 AM
1
Ashley 193.7k rep

@Matiu - the signalling server uses secure websockets, not https. The connection is already secure (since it uses wss: instead of ws:).

Monday, March 17, 2014 at 4:53:33 PM
2
Matiu 921 rep

Thanks Ashley, I have since been reading up on wss:)

Tuesday, March 18, 2014 at 7:49:36 AM
4
Miu3 7,561 rep

Can you maybe later describe more the signaling server part? Can we use this address for our own projects forever? Any possible tutorials for other alternatives?

Wednesday, March 19, 2014 at 10:00:58 PM
4
fassFlash 4,377 rep

Civilization V uses a system where when the host leaves the game, another player with the best connection/at random gets the host position. Can you implement this in the example?

Sunday, March 23, 2014 at 10:01:42 AM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.