Online Multiplayer in NodeJS
So you've decided to venture into the realms of Online Multiplayers. Why not? You can get all you friends involved in the projects you make, or just show it off to others. Well, either way, let's take a look at how we would make one.
Firstly, we are going to need some Modules to help us along the way. Let's see:
That should do the trick! We have:
expressto display the static site to the Clients
httpto listen for new Clients
socket.ioto listen for events the Clients make
Simples! So now that we know what we are working with, let's make this Game!
Here are the HTML and CSS before we begin, as they aren't super important, I won't explain them too much:
A good thing to not is that you should not forget the
<script src="/socket.io/socket.io.js"></script>. this is super important!
This just makes it look nice. It also fills the background with the same as the canvas, and center said canvas in the middle of the window.
I'd say that the Client is always best to work on first. The reason for this, is that you can begin to layout what you are going to be using. This means you know what you will need on the Server before you start writing it. Obviously you will be adding stuff later, but it's a great way to begin the project. So let's begin:
Firstly, we want to get our Client socket to communicate with the Server. Now we can create the
This is nothing special. All we do is define our
setup function and
600x600 canvas. We also remove the outline around our players by using
Next we define our
D keys. The numbers
68 refer to each key's code. A good way of finding out a key's code is by using this website. All you have to do is press the key you want to get the code of, the nice thing is that the favicon of the page will change to the code's number so that it easier to see when you switch tabs:
Now we get into the nitty gritty. We create an event called
update which we will talk more about in the Server section. This event will give us the Game's current data to update the Client-Side with. talking about that:
We change the background color to
background(). We then check if either of
D is down, if so, we send an event to the Server called
move. We also send the key with it. Next:
We get all of the
ids for all the players in
data.players, we then get the
player for the
id and show them on the canvas. This allows us to see all the players. But what about us?
I'm glad you asked! Well, we do draw ourselves here, we do it along with the others. Because all the updating is done Server-Side (which may change depending on how you implement it), the only thing we do is get movement events, and draw the players on the Client-Side.
This is it for the Client-Side of things. Super simple! So let's get onto the Server-Side of things!
The server will now have to do a few things based on how we wrote the Client. These include:
- Sending an
updateevent to the Clients
moveevents from the Clients
- Updating the players
Not too much, so let's get cracking! Oh, and we covered Socket.io in another tutorial, so I won't be diving to far in in this one:
We first want to make it so that the Client is given the Game website. Then we can:
We define a few variables and constants. These include the movement keys from earlier, the
data including the
players, and the
height. Not too much so far:
Firstly, we get the
disconnect events. When we get a
connection event, we create a new player and give it the
socket.id. On the
disconnect event, we delete the player from
data.players using the
Next we get the
move event. In this event, we check to see if any of the movement keys are pressed. We do this, just in case the Client sends us something which is not a movement key, think of it as extra security. If one of the keys is pressed, we add or subtract the
player.speed from the
y. We then also check to see if the player is at one of the boundaries. If so, then we stop the player from passing it. That's it!
It's pretty easy when you look at it. You also have the extra security of having things done Server-Side which is nice. The player stuff could be done better, but works great for a little "test" online game. Hope it helps in your ventures!
Have a great day!
If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!
If I use a nodeJS repl here on repl.it, it won't show the code, only the nodeJS console. What should I do?
@stringycheese if your on phone zoom out and/or scroll down and youo will see some options, im on a chromebook sometimes (like right now) and a windows 10 computer other times and it is working fine for me
if you want the webpage html you need to do some code first to set the port and to set it to show the html, and from there you can ut the jvvascript into a script part of the html, although sadly it sees you cant make it run another script file, you need to write it all in the html, and also there is no check for syntax it seems. But it seems the page reloads whenever it tries to run a script with wrong syntax, so you can keep an eye out for that.
an error is reported on line 39:7, in the JS file. it says that the key codes have already been defined (in line 15-18). For some reason, when I remove the error, it displays the canvas wrong.