Here we'll be going over the steps to get your Unity client up and running and connected to a Colyseus server.
Topics covered include:
Running the server locally
Server settings
Connecting to a server
Connecting to a room
Communicating with a room, and the room's state.
The topics should be enough for you to set up a basic client on your own, however, you are welcome to use and modify the included example code to suit your needs.
To run the demonstration server locally, you will first need to download the Server code to your machine. The Server code for the Unity SDK lives at https://github.com/colyseus/colyseus-unity-sdk/tree/master. Download that repo, and open a terminal in the repo folder, then run the following commands in your terminal:
cd Server
npm install
npm start
The built-in demonstration comes with a single room handler, containing a suggested way of handling entities and players. Feel free to change all of it to fit your needs!
There are several ways to create and/or join a room.
You can create a room by calling the Create method of ColyseusClient which will automatically create an instance of the room on the server and join it:
You can call the JoinOrCreate method of ColyseusClient which will matchmake into an available room, if able to, or will create a new instance of the room and then join it on the server:
When creating a new room you have the ability to pass in a dictionary of room options, such as a minimum number of players required to start a game or the name of the custom logic file to run on your server.
Options are of type object and are keyed by the type string:
Each room holds its own state. The mutations of the state are synchronized automatically to all connected clients.
In regards to room state synchronization:
When the user successfully joins the room, they receive the full state from the server.
At every patchRate, binary patches of the state are sent to every client (default is 50ms)
onStateChange is called on the client-side after every patch received from the server.
Each serialization method has its own particular way to handle incoming state patches.
MyRoomState is the base room state you will want your room state to inherit from.
Take a look at our tech demos for implementation examples of synchronizable data in a room's state such as networked entities, networked users, or room attributes. (Shooting Gallery Tech Demo)
When you are using MapSchema or ArraySchema, the state sychronizations from the server can be used as below as well.
// Something has been added to Schemaroom.State.players.OnAdd((key,player)=>{Debug.Log($"{key} has joined the Game!");});// Something has changed in Schemaroom.State.players.OnChange((key,player)=>{Debug.Log($"{key} has been changed!");});// Something has been removed from Schemaroom.State.players.OnRemove((key,player)=>{Debug.Log($"{key} has left the Game!");});
If you set a breakpoint in your application while the WebSocket connection is open, the connection will be closed automatically after 3 seconds due to inactivity. To prevent the WebSocket connection from dropping, use pingInterval: 0 during development: