Room API (Client-side)¶
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.
sessionId: string
¶
Unique identifier for the player. 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 (data)
¶
Send message to the room handler.
room.send({ move: "left" });
await room.Send(new { move = "left" });
room:send({ move = "left" })
room.send({ move: "left" });
Use Room#onMessage() from the server-side to read the message.
leave ()
¶
Disconnect from the room.
room.leave();
room.Leave();
room:leave()
room.leave();
Tip
Use Room#onLeave() to handle the disconnection from the server-side.
removeAllListeners()
¶
Removes onMessage
, onStateChange
, onLeave
and onError
listeners.
Also removes all .listen()
calls if you're using Fossil Delta serializer..
Events¶
onStateChange¶
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.
room.onMessage((message) => {
console.log("message received from server");
console.log(message);
});
room.OnMessage += (message) => {
Debug.Log ("message received from server");
Debug.Log(message);
}
/**
* Handling schema-encoded messages:
*/
room.OnMessage += (message) => {
if (message is MyMessage)
{
Debug.Log ("MyMessage type has been received");
Debug.Log(message);
}
else if (message is AnotherMessage) {
// ...
}
}
room:on("message", function(message)
print("message received from server")
print(message)
end)
room.onMessage += function(message) {
trace("message received from server");
trace(Std.string(message));
};
room.onMessage = [=](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 room.send() or room.broadcast()
onLeave¶
This event is triggered when the client leave the room.
Possible codes:
1000
: Regular Socket Shutdown- higher than
1000
: Abnormal Socket Shutdown (more details)
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;
};
onError¶
This event is triggered when some error occurs in the room handler.
room.onError((message) => {
console.log("oops, error ocurred:");
console.log(message);
});
room.OnError += (message) => {
Debug.Log ("oops, error ocurred:");
Debug.Log(message);
}
room:on("error", function(message)
print("oops, error ocurred:")
print(message)
end)
room.onError += function(message) {
trace("oops, error ocurred:");
trace(message);
};
room.onError = [=] (std::string message) => void {
std::cout << "oops, error ocurred: " << message << std::endl;
};