In this entry of the "Creating a Multiplayer Game Server" series we will be covering an Overview of WebSocket.
In the previous part of this series, I covered the basics of Client-Server communications and explained that a persistent connection was going to be needed for our multiplayer game server. I also shared that we would be using WebSocket establishing our persistent connection so it's very important for us to have a basic understanding of WebSocket and what functionalities it has.
So in this blog, it is my goal to briefly explain what WebSocket is and some of the core functionality we will be using in our game server (and also our client).
So, let's get started....
WebSocket is a communications protocol that allows for full-duplex communication over a single TCP connection (Source: wikipedia. In short, WebSocket allows persistent communication between a server and a client over a single connection.
WebSocket is also been used for a very long time in various implementations so it benefits from a large amount of example code and developer community engagement.
One of the really nice things about WebSocket are the numerous implementations it has in different coding languages. In our case, we will be using the C# implementation of WebSocket in Unity3D (our client) and we will be using a Node.js implementation on our server. Though the implementations will be slightly different from a "syntactical" standpoint, the functionality will be essentially the same.
Now that we have a basic understanding of what WebSocket is, let's look at some of the core functionality that WebSocket offers that we will be using within our game server and our client. NOTE: The exact names of these functionalities may differ from implementation to implementation, but my goal is to use the most general terminology to capture the intent of functionalities.
The first thing to note about WebSocket is that it's broken down in to two different types of capabilities: Event Methods and Event Listeners.
Event Methods are functions/methods that trigger events within WebSocket. These methods are the mechanisms in which client and server "talk" to each other. For Example, the client can trigger an event that sends data to the server.
Event Listeners are functions/methods that listen for events that have been triggered within WebSocket. The Listeners are the mechanisms that client and server use to "listen" to each other. For example, the server can have a listener that "listens" for when data is being sent from the client.
So let's break down the specific Event Methods and Event Listeners that WebSocket offers:
Connect: An event method that establishes a persistent connection. This method is normally triggered by the client to "connect" to the server and starts the "handshake" that is needed for the client and server to become connected.
Send: An event method that sends message data in different formats(e.g. text or binary). This method is used by both the server and the client and is the method that is used most often in WebSocket. It essentially is the mechanism that allows the server and client to pass data.
Close: An event method that fires when a connection is closed. This is often used to manually close a connection. It is a great method for allowing clients to smoothly disconnect from servers.
OnOpen: An event listener that is called when an initial connection is opened/established. This tends to be called when a "Connect" Event method is triggered successfully. This generally is used to confirm that a connection has been successfully connected.
OnConnection: An event listener that manages the persistent connection with a client. This is normally a listener used by the server. This listener acts as the continuous listener that allows other listeners to be called when needed (This will make more sense when we get into the code 😁).
OnMessage: An event listener called when a message is received. This is normally triggered by the "Send" event method. This listener is hugely important and is generally used to receive data and parse it as needed. This is used heavily by both client and server.
OnClose: An event listener called when a connection is closed. This method can be called manually through the "Close" event method or through any method of disconnection, such as network timeout. It is primarily used on the server to manage clients that disconnect but can also be used on the client in cases where the server forcibly disconnects.
OnError: An event listener called when an error occurs. This listener is triggered by any error event. This is where all WebSocket related error handling is managed.
As we now can see, WebSocket has a lot of capabilities that make managing persistent connections fairly straight-forward. In many ways, the best ways to understand these methods and listeners is to use them. Some of these may seem a bit "out there" at first, but once we start building our server, their use and importance will become more apparent.
Though we covered a lot of functionality of WebSocket (primarily functionalities that we will need to build our server), there as still other functionalities that I did not cover in this blog. If you are interested in learning more about WebSocket, check out the WebSocket Wikipedia Article and the webSocket API Documentation
And with that, we are all finished with our WebSocket Overview. In our next blog, we will be getting into the nitty-gritty of this series; the code for building our game server 🔥🔥. We will be covering the first steps of building our server and using some of knowledge we have learned so far. I personally am very much looking forward to sharing what I have learned with you all. Because, let's be honest, the code is the best part. 😅
As always, thank you for checking out this blog and looking forward to sharing some more knowledge with you all, very soon!
Onwards and Upwards!
==== FOLLOW ME ON SOCIAL MEDIA ====