Client-side SDK¶
Colyseus currently has client-side SDK's available for the following platforms:
- Unity (view source-code)
- JavaScript/TypeScript (view source-code)
- Defold Engine (view source-code)
- Haxe (view source-code)
- Construct3 (view source-code)
- Cocos Creator
The Client Instance:¶
The Client
instance is used to perform matchmaking calls, and later connect to one or many rooms.
import Colyseus from "colyseus.js";
// ...
let client = new Colyseus.Client("ws://localhost:2567");
using Colyseus;
// ...
ColyseusClient client = new ColyseusClient("ws://localhost:2567");
local ColyseusClient = require("colyseus.client")
// ...
local client = ColyseusClient.new("ws://localhost:2567");
import io.colyseus.Client;
// ...
var client = new Client("ws://localhost:2567");
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.
try {
const room = await client.joinOrCreate("battle", {/* options */});
console.log("joined successfully", room);
} catch (e) {
console.error("join error", e);
}
client.joinOrCreate("battle", {/* options */}).then(room => {
console.log("joined successfully", room);
}).catch(e => {
console.error("join error", e);
});
try {
Room<YourStateClass> room = await client.JoinOrCreate<YourStateClass>("battle", /* Dictionary of options */);
Debug.Log("joined successfully");
} catch (ex) {
Debug.Log("join error");
Debug.Log(ex.Message);
}
client:join_or_create("battle", {--[[options]]}, function(err, room)
if (err ~= nil) then
print("join error: " .. err)
return
end
print("joined successfully")
end)
client.joinOrCreate("battle", [/* options */], YourStateClass, function(err, room) {
if (err != null) {
trace("join error: " + err);
return;
}
trace("joined successfully");
});
client->joinOrCreate<YourStateClass>("battle", {/* options */}, [=](std::string err, Room<State>* room) {
if (err != "") {
std::cout << "join error: " << err << std::endl;
return;
}
std::cout << "joined successfully" << std::endl;
});
create (roomName: string, options: any)
¶
Creates a new room by provided roomName
and options
.
try {
const room = await client.create("battle", {/* options */});
console.log("joined successfully", room);
} catch (e) {
console.error("join error", e);
}
client.create("battle", {/* options */}).then(room => {
console.log("joined successfully", room);
}).catch(e => {
console.error("join error", e);
});
try {
Room<YourStateClass> room = await client.Create<YourStateClass>("battle", /* Dictionary of options */);
Debug.Log("joined successfully");
} catch (ex) {
Debug.Log("join error");
Debug.Log(ex.Message);
}
client:create("battle", {--[[options]]}, function(err, room)
if (err ~= nil) then
print("join error: " .. err)
return
end
print("joined successfully")
end)
client.create("battle", [/* options */], YourStateClass, function(err, room) {
if (err != null) {
trace("join error: " + err);
return;
}
trace("joined successfully");
});
client->create<YourStateClass>("battle", {/* options */}, [=](std::string err, Room<State>* room) {
if (err != "") {
std::cout << "join error: " << err << std::endl;
return;
}
std::cout << "joined successfully" << std::endl;
});
join (roomName: string, options: any)
¶
Joins an existing room by provided roomName
and options
.
Locked or private rooms are ignored by this method.
try {
const room = await client.join("battle", {/* options */});
console.log("joined successfully", room);
} catch (e) {
console.error("join error", e);
}
client.join("battle", {/* options */}).then(room => {
console.log("joined successfully", room);
}).catch(e => {
console.error("join error", e);
});
try {
Room<YourStateClass> room = await client.Join<YourStateClass>("battle", /* Dictionary of options */);
Debug.Log("joined successfully");
} catch (ex) {
Debug.Log("join error");
Debug.Log(ex.Message);
}
client:join("battle", {--[[options]]}, function(err, room)
if (err ~= nil) then
print("join error: " .. err)
return
end
print("joined successfully")
end)
client.join("battle", [/* options */], YourStateClass, function(err, room) {
if (err != null) {
trace("join error: " + err);
return;
}
trace("joined successfully");
});
client->join<YourStateClass>("battle", {/* options */}, [=](std::string err, Room<State>* room) {
if (err != "") {
std::cout << "join error: " << err << std::endl;
return;
}
std::cout << "joined successfully" << std::endl;
});
joinById (roomId: string, options: any)
¶
Joins an existing room by its roomId
. Private rooms can be joined by id.
try {
const room = await client.joinById("KRYAKzRo2", {/* options */});
console.log("joined successfully", room);
} catch (e) {
console.error("join error", e);
}
client.joinById("KRYAKzRo2", {/* options */}).then(room => {
console.log("joined successfully", room);
}).catch(e => {
console.error("join error", e);
});
try {
Room<YourStateClass> room = await client.JoinById<YourStateClass>("battle", /* Dictionary of options */);
Debug.Log("joined successfully");
} catch (ex) {
Debug.Log("join error");
Debug.Log(ex.Message);
}
client:join_by_id("battle", {--[[options]]}, function(err, room)
if (err ~= nil) then
print("join error: " .. err)
return
end
print("joined successfully")
end)
client.joinById("battle", [/* options */], YourStateClass, function(err, room) {
if (err != null) {
trace("join error: " + err);
return;
}
trace("joined successfully");
});
client->joinById<YourStateClass>("battle", {/* options */}, [=](std::string err, Room<State>* room) {
if (err != "") {
std::cout << "join error: " << err << std::endl;
return;
}
std::cout << "joined successfully" << std::endl;
});
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.reconnectionToken
from 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.
try {
const room = await client.reconnect(cachedReconnectionToken);
console.log("joined successfully", room);
} catch (e) {
console.error("join error", e);
}
client.reconnect(cachedReconnectionToken).then(room => {
console.log("joined successfully", room);
}).catch(e => {
console.error("join error", e);
});
try {
Room<YourStateClass> room = await client.Reconnect<YourStateClass>(cachedReconnectionToken);
Debug.Log("joined successfully");
} catch (ex) {
Debug.Log("join error");
Debug.Log(ex.Message);
}
client:reconnect(cached_reconnection_token, function(err, room)
if (err ~= nil) then
print("join error: " .. err)
return
end
print("joined successfully")
end)
client.reconnect(cachedReconnectionToken, YourStateClass, function(err, room) {
if (err != null) {
trace("join error: " + err);
return;
}
trace("joined successfully");
});
getAvailableRooms (roomName?: string)
¶
Queries for all available rooms to connect.
- Locked and private rooms won't be listed.
- If the
roomName
parameter is ommitted, all rooms are going to be queried.
client.getAvailableRooms("battle").then(rooms => {
rooms.forEach((room) => {
console.log(room.roomId);
console.log(room.clients);
console.log(room.maxClients);
console.log(room.metadata);
});
}).catch(e => {
console.error(e);
});
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);
client:get_available_rooms("battle", function(err, rooms)
if (err) then
console.error(err);
return
end
for i, room in pairs(rooms) do
print(room.roomId)
print(room.clients)
print(room.maxClients)
print(room.metadata)
end
end);
client.getAvailableRooms("battle", function(err, rooms) {
if (err != null) {
trace(err);
return;
}
for (room in rooms) {
trace(room.roomId);
trace(room.clients);
trace(room.maxClients);
trace(room.metadata);
}
});
client.getAvailableRooms("battle", [=](std::string err, nlohmann::json rooms) {
if (err != "") {
std::cout << "error: " << err << std::endl;
return;
}
// rooms
});
consumeSeatReservation (reservation)
¶
Join a room by manually consuming a "seat reservation".
try {
const room = await client.consumeSeatReservation(reservation);
console.log("joined successfully", room);
} catch (e) {
console.error("join error", e);
}
client.consumeSeatReservation(reservation).then(room => {
console.log("joined successfully", room);
}).catch(e => {
console.error("join error", e);
});
try {
Room<YourStateClass> room = await client.ConsumeSeatReservation<YourStateClass>(reservation);
Debug.Log("joined successfully");
} catch (ex) {
Debug.Log("join error");
Debug.Log(ex.Message);
}
client:consume_seat_reservation(reservation, function(err, room)
if (err ~= nil) then
print("join error: " .. err)
return
end
print("joined successfully")
end)
client.consumeSeatReservation(reservation, YourStateClass, function(err, room) {
if (err != null) {
trace("join error: " + err);
return;
}
trace("joined successfully");
});
client->consumeSeatReservation<YourStateClass>(reservation, [=](std::string err, Room<State>* room) {
if (err != "") {
std::cout << "join error: " << err << std::endl;
return;
}
std::cout << "joined successfully" << std::endl;
});
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.
//
// sending message with string type
//
room.send("move", { direction: "left"});
//
// sending message with number type
//
room.send(0, { direction: "left"});
//
// sending message with string type
//
await room.Send("move", new { direction = "left" });
//
// sending message with number type
//
await room.Send(0, new { direction = "left" });
--
-- sending message with string type
--
room:send("move", { direction = "left" })
--
-- sending message with number type
--
room:send(0, { direction = "left" })
//
// sending message with string type
//
room.send("move", { direction: "left" });
//
// sending message with number type
//
room.send(0, { direction: "left" });
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).
//
// sending message with number type
//
room.send(0, [ 172, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33 ]);
//
// sending message with string type
//
room.send("some-bytes", [ 172, 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33 ]);
leave (consented: boolean)
¶
Disconnect client from the room.
Parameters
consented
: Whether the act of leaving has been "consented" or not (Default istrue
)
// consented leave
room.leave();
// unconsented leave
room.leave(false);
// consented leave
room.Leave();
// unconsented leave
room.Leave(false);
-- consented leave
room:leave()
-- unconsented leave
room:leave(false)
// consented leave
room.leave();
// unconsented leave
room.leave(false);
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.
room.onStateChange.once((state) => {
console.log("this is the first room state!", state);
});
room.onStateChange((state) => {
console.log("the room state has been updated:", state);
});
room.OnStateChange += (state, isFirstState) => {
if (isFirstState) {
Debug.Log ("this is the first room state!");
}
Debug.Log ("the room state has been updated");
}
room:on("statechange", function(state)
print("new state:", state)
end)
room.onStateChange += function(state) {
trace("new state:" + Std.string(state));
};
room.onStateChange = [=](State>* state) {
std::cout << "new state" << std::endl;
// ...
};
onMessage¶
This event is triggered when the server sends a message directly to the client, or via broadcast.
room.onMessage("powerup", (message) => {
console.log("message received from server");
console.log(message);
});
class PowerUpMessage {
string kind;
}
room.OnMessage<PowerUpMessage>("powerup", (message) => {
Debug.Log ("message received from server");
Debug.Log(message);
});
room:on_message("powerup", function(message)
print("message received from server")
print(message)
end)
room.onMessage("powerup", function(message) {
trace("message received from server");
trace(Std.string(message));
});
room.onMessage("powerup", [=](msgpack::object message) -> void {
std::cout << "message received from server" << std::endl;
std::cout << message << std::endl;
});
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.
room.onLeave((code) => {
console.log("client left the room");
});
room.OnLeave += (code) => {
Debug.Log ("client left the room");
}
room:on("leave", function()
print("client left the room")
end)
room.onLeave += function () {
trace("client left the room");
};
room.onLeave = [=]() -> void {
std::cout << "client left the room" << std::endl;
};
Possible closing code
s and their meaning:
1000
- Regular socket shutdown- Between
1001
and1015
- Abnormal socket shutdown - Between
4000
and4999
- Custom socket close code (See more details)
onError¶
This event is triggered when some error occurs in the room handler.
room.onError((code, message) => {
console.log("oops, error ocurred:");
console.log(message);
});
room.OnError += (code, message) => {
Debug.Log ("oops, error ocurred:");
Debug.Log(message);
}
room:on("error", function(code, message)
print("oops, error ocurred:")
print(message)
end)
room.onError += function(code, message) {
trace("oops, error ocurred:");
trace(message);
};
room.onError = [=] (int code, std::string message) => void {
std::cout << "oops, error ocurred: " << message << std::endl;
};