Skip to content

Web-Socket Client

The client instance present on:

Note

This is the raw WebSocket connection coming from the ws package. There are more methods available which aren't encouraged to use along with Colyseus.

Properties

sessionId: string

Unique id per session.

Note

In the client-side, you can find the sessionId in the room instance.

auth: any

Custom data you return during onAuth().

Methods

send(type, message)

Send message a type of message to the client. Messages are encoded with MsgPack and can hold any JSON-seriazeable data structure.

The type can be either a string or a number.

Sending a msgpack-encoded message:

//
// sending message with string type
//
client.send("powerup", { type: "ammo" });

//
// sending message with number type
//
client.send(1, { type: "ammo"});

Sending a schema-encoded message:

Sending schema-encoded messages is particularly useful for statically-typed languages such as C#.

class MyMessage extends Schema {
  @type("string") message: string;
}

const data = new MyMessage();
data.message = "Hello world!";

client.send(data);

leave(code?: number)

Force disconnection of the client with the room.

Tip

This will trigger room.onLeave event on the client-side.

error(code, message)

Send an error with code and message to the client. The client can handle it on onError