How to build a multiplayer game - RustFest Global 2020 Pre-Event

Sketch of one Tick As announced on the rust zürisee meetup: I gave a talk about How to build a multiplayer game with actix-web that people with any modern browser shipping JavaScript, Canvas Context2D and Websocket can play.

You can watch the recording on Youtube now.

Play the game

You can play my MultiPlayer Snake on with your browser.

Challenges during deployments

While I did plan to finish development before the conference, automating the deployment is always handy because:

  1. No more human error
  2. Even in a stressful situation after something has happened, the deployment will working and not introduce new problems

CSP: content-security-policy

Quotations are opposite to most programming languages

There are two important rules:

  1. Keywords are in single quotes ' but Strings are not quoted
  2. Directives are separated by a simple space  no colon

Connect-src 'self' does not cover wss://self

In the docs there is a very innocent sentence:

... the same URL scheme and port number.

This means for a connection over HTTPS 'self' does not cover wss:// even though the WebSocket connection is setup over HTTPS. However, fetch('/...') for an API is allowed. Allowing the client to send an error message when it is unable to open the WebSocket.

So the final policy now includes the hostname for the WebSocket and the frame-ancestors for RustFest Global:

default-src 'none'; frame-ancestors https://*; connect-src 'self' wss://; font-src 'self'; img-src 'self' data:; script-src 'self'; style-src 'self'

Deployment from Arch to Debian

Using Arch Linux is very nice for development, because like Gentoo it ships with all the latest software but unlike Gentoo you don't have to spend hours compiling everything from source. To me this compromise means: * I can use the latest compilers within a week of their release without having to check all their announcements * My software is automatically future prove as the other distros will update to this feature set within the next 6 to 24 months * The setup is rather minimal because I have to install everything myself

On the other hand, I operate servers that I don't want to update all the time and prefere more stable software. The perfect match for me since 2006 is Debian and with systemd I get a service watchdog for free.

However, after transferring the compiled project to the host reveals problem:

./multiplayer_snake: /lib/x86_64-linux-gnu/ version `GLIBC_2.32' not found (required by ./multiplayer_snake)

Building with Docker

Cross-Compiling for an older libc is currently not that simple. So building in a VM or a container is much more comfortable.

I would love to switch to the root-less podman, but if you have to use docker: Don't forget to pass the --user flags or run the command with sudo:

docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/src/myapp rust:latest cargo build --release

Don't forget to pull rust:latest after every release of the rust compiler with:

docker pull rust:latest