Client-side SDK¶
Colyseus currently has official client-side SDKs for the following platforms:
See Unnoficial SDKs by the community for Unreal Engine, Godot and others.
The Client Instance:¶
The Client instance is used to perform matchmaking calls, and later connect to one or many rooms.
No connection with the server is established yet by creating a Client instance.
Methods¶
joinOrCreate (roomName: string, options: any)¶
Join an existing room or create a new one, by provided roomName and options.
Locked or private rooms are ignored by this method.
create (roomName: string, options: any)¶
Creates a new room by provided roomName and options.
join (roomName: string, options: any)¶
Joins an existing room by provided roomName and options.
Locked or private rooms are ignored by this method.
joinById (roomId: string, options: any)¶
Joins an existing room by its roomId. Private rooms can be joined by id.
Getting available roomId's you can join
See getAvailableRooms() to retrieve a list of rooms with their respective roomId's available for joining, along with their metadata.
reconnect (reconnectionToken)¶
Reconnects the client back into a previously connected room.
- You must store/cache the
room.reconnectionTokenfrom an active room connection to be able to reconnect. - To enable the reconnection of a particular client, the server-side needs to call
.allowReconnection()for that client instance.
getAvailableRooms (roomName?: string)¶
Queries for all available rooms to connect.
- Locked and private rooms won't be listed.
- If the
roomNameparameter is ommitted, all rooms are going to be queried.
try {
var rooms = await client.GetAvailableRooms("battle");
for (int i = 0; i < rooms.Length; i++) {
Debug.Log(rooms[i].roomId);
Debug.Log(rooms[i].clients);
Debug.Log(rooms[i].maxClients);
Debug.Log(rooms[i].metadata);
}
} catch (ex) {
Debug.Log(ex.Message)
}
/**
* Retrieving custom metadata
*/
[Serializable]
class Metadata
{
public string mode;
public string name;
}
[Serializable]
class CustomRoomAvailable : RoomAvailable
{
public Metadata metadata;
}
var rooms = await client.GetAvailableRooms<CustomRoomAvailable>("battle");
Debug.Log(rooms[0].metadata.mode);
consumeSeatReservation (reservation)¶
Join a room by manually consuming a "seat reservation".
Advanced usage
See Match-maker API to learn how to manually reserve a seat for a client within a room.
The Room Instance:¶
Properties¶
state: any¶
The current room's state. This variable is always synched with the latest
state from the server-side. To listen for updates on the whole state, see
onStateChange event.
You may attach callbacks to specific structures inside your state. See schema callbacks.
sessionId: string¶
Unique identifier for the current connected client. This property matches the client.sessionId from the server-side.
id: string¶
The unique idenfitier of the room. You can share this id with other clients in order to allow them to connect directly to this room.
// get `roomId` from the query string
let roomId = location.href.match(/roomId=([a-zA-Z0-9\-_]+)/)[1];
// joining a room by its id
client.joinById(roomId).then(room => {
// ...
});
name: string¶
Name of the room handler. Ex: "battle".
Methods¶
send (type, message)¶
Send a type of message to the room handler. Messages are encoded with MsgPack and can hold any JSON-serializable data structure.
Use Room#onMessage() from the server-side to receive the messages
Check out Server-side API » Room - onMessage() section.
sendBytes (type, bytes)¶
Send a raw byte array as a message to the server. A byte array is an array of numbers from 0 to 255.
This is useful if you'd like to manually encode a message, rather than the default encoding (MsgPack).
leave (consented: boolean)¶
Disconnect client from the room.
Parameters
consented: Whether the act of leaving has been "consented" or not (Default istrue)
Tip
Use Room#onLeave() to handle the disconnection from the server-side.
removeAllListeners()¶
Removes onMessage, onStateChange, onLeave and onError listeners.
Events¶
onStateChange¶
You may trigger callbacks for specific Schema structures
Check out the State Handling » Schema » Client-side section for more details.
This event is triggered when the server updates its state.
onMessage¶
This event is triggered when the server sends a message directly to the client, or via broadcast.
Tip
To send a message from the server directly to the clients you'll need to use either client.send() or room.broadcast()
onLeave¶
This event is triggered when the client leave the room.
Possible closing codes and their meaning:
1000- Regular socket shutdown- Between
1001and1015- Abnormal socket shutdown - Between
4000and4999- Custom socket close code (See more details)
onError¶
This event is triggered when some error occurs in the room handler.