4 Making a multiplayer client

 

This chapter covers

  • Establishing a connection in our game client
  • Handling messages received from our backend
  • Sending messages to all connected clients
  • Leveraging Rust’s types to write code that is clear and maintainable

In chapter 2, we built a simple single-player game client in which we could fly a little plane around on our screen. In chapter 3, we put together a server which can track connections made to it, as well as send and receive messages. In this chapter, we are going to begin by modifying the single-player game client that we’ve already created to enable online multiplayer play. To do this, our game client will need to be able to establish a connection to our server, it will need to receive messages, as well as send messages to the server, so that other connected clients may be informed about updates to the game’s state.

Once communication is established between our game clients and server, this is where we can start to shape our game’s behaviour based on the input of other players. When we receive a message from our server that another player has joined the game, we should expect to see a graphic representing that new player in our client. When the server sends us an update about the state of another client’s plane, we should handle that by moving the plane to reflect that state, and whatever else that information might entail.

4.1 Establishing a Connection

4.1.1 Setting up the Connection to the Server

4.1.2 Polling the Server for Messages

4.2 Receiving messages from the Server

4.2.1 Receiving messages in the game loop

4.2.2 Deserializing received messages

4.2.3 Handling received messages

4.3 Sending messages to the Server

4.3.1 Sending the state message

4.4 Finishing Touches

4.5 Summary