Skip to content

Overview

Colyseus currently have clients for the platforms:

Need a client for another platform? Share your interest on the discussion board!

Connecting to the Server

import * as Colyseus from "colyseus.js";
// ...

let client = new Colyseus.Client("ws://localhost:2657");
using Colyseus;
// ...

Client client = new Client("ws://localhost:2657");
local ColyseusClient = require("colyseus.client")
// ...

local client = ColyseusClient.new("ws://localhost:2657");

Methods

join (roomName: string, options: any)

let room = client.join("battle");
Room room = client.Join("battle");
local room = client:join("battle")

getAvailableRooms (roomName: string)

List all available rooms to connect with the provided roomName. Locked rooms won't be listed.

client.getAvailableRooms("battle", function(rooms, err) {
  if (err) console.error(err);
  rooms.forEach(function(room) {
    console.log(room.roomId);
    console.log(room.clients);
    console.log(room.maxClients);
    console.log(room.metadata);
  });
});
client.GetAvailableRooms("battle", (rooms: RoomsAvailable, err: ErrorEventArgs) => {
  rooms.
});
client:getAvailableRooms("battle", function(rooms, err)
  if (err) console.error(err);
  for i, rooms in pairs(rooms) do
    print(rooms[i].roomId)
    print(rooms[i].clients)
    print(rooms[i].maxClients)
    print(rooms[i].metadata)
  end
end);

close ()

Close connection with the server.

client.close();
client.Close();
client:close()

Events

onOpen

This event is triggered when the connection is accepted by the server.

client.onOpen.add(function() {
  console.log("connection is now open");
});
client.OnOpen += (object sender, EventArgs e) => {
  Debug.Log ("connection is now open");
}
client:on('open', function()
  print("connection is now open")
end)

onClose

This event is triggered when the connection is closed.

client.onClose.add(function() {
  console.log("connection has been closed");
});
client.OnClose += (object sender, EventArgs e) => {
  Debug.Log ("connection has been closed");
}
client:on('close', function()
  print("connection has been closed")
end)

onError

This event is triggered when some error occurs in the server.

client.onError.add(function(err) {
  console.log("something wrong happened", err);
});
client.OnError += (object sender, EventArgs e) => {
  Debug.Log ("something wrong happened");
}
client:on("error", function()
  print("something wrong happened")
end)

Properties

id: string

Unique identifier for the client.

Note

The same client id can connect into the same room handler when joining from multiple browser tabs.