POC for a Unity game client and game server with websockets!
There are 2 implementations of the game server. One in Python and one in Golang. The APIs for both servers are identical.
- Unity Editor:
version 2020.3.21f1
- Docker
- Docker Compose
- ngrok (optional)
- Build and spin-up one of the websocket servers.
python
docker build -t python-gameserver:latest ./PythonGameServer
docker-compose -f ./PythonGameServer/docker-compose.yaml up
golang
docker build -t golang-gameserver:latest ./GolangGameServer
docker-compose -f ./GolangGameServer/docker-compose.yaml up
- Open the Unity editor and run the game.
The players is simply a circle you can move around with WASD. Other connected players are red and the main player is white.
-
Build and spin-up one of the game servers locally.
-
Expose to the internet with ngrok.
ngrok tcp 5000
- Copy the ngrok URL to the game client, but replace the prefix
tcp
with the prefixws
.
Now you can connect multiple players who can see each other's movement!
This is a very simple proof-of-concept and could be used as a starting point for making a multiplayer game with the Unity engine.
It demonstrates game-client to game-server connection establishment, message transmission, and connection termination with the websocket protocol.
The entire game-server's code resides in:
PythonGameServer/server.py
(python server)
GolangGameServer/server.go
(golang server)
The game-client's websocket management resides in:
UnityWebSocketClient/Assets/Scripts/SceneManagerScript.cs
The Golang game server's architecture is inspired by this chat example.
Enjoy!