Multiplayer tutorial 2: chat room

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

Chat events

At the top of the event sheet are some global variables:

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


It is good practice to have global constants for the game name, instance name and room name if they do not change. This means there is only one place we need to update our project if we wanted to change any of them. In this case all three are always the same: everyone who uses the app joins the same chat room. If we changed any of them, the user would end up joining a different chat room. However if we wanted to make separate chat rooms it would be most sensible to put them in a different signalling room: game instances are intended for different versions of the same app, and the game name should always be the same for the same project.

Chat log group

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


This group simply has a function to add a new line of text to the chat log, and then scroll it to the bottom. Since we update the chat log in a lot of places, it makes sense to have a single function do this instead of repeating the actions over and over again. Also after several chat messages the content of the object could go off the bottom of the object, so we always scroll to the bottom so the latest messages are in view.

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.9k 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.7k 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.9k 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,565 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 195.4k 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,571 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.