https://docs.colyseus.io/server/client/#sendtype-message
send(type, message)
I suggest not to call this "type" because "type" has a meaning in JavaScript/TypeScript (pun intended, proving my point). Maybe call it "messageId", similar to "roomId" or "clientId".
//
// sending message with string type
//
client.send("powerup", { type: "ammo" });
//
// sending message with number type
//
client.send(1, { type: "ammo"});
Regarding { type: "ammo" }
I suggest not to call this "type" because of the very same reason and because it currently can be mistaked for the "type" mentioned above. Maybe call it "poweruptype" or "category" or any other synonym for "type".
class MyMessage extends Schema {
@type("string") message: string;
}
const data = new MyMessage();
data.message = "Hello world!";
client.send(data);
Why does client.send
have no "type" (in the sense of "messageId") as the first parameter?
https://docs.colyseus.io/client/room/#onmessage
C# example
class PowerUpMessage {
string type;
}
room.OnMessage<PowerUpMessage>("powerup", (message) => {
Debug.Log ("message received from server");
Debug.Log(message);
});
/**
* Handling schema-encoded messages:
*/
room.OnMessage("powerup", (message) => {
if (message is MyMessage)
{
Debug.Log ("MyMessage type has been received");
Debug.Log(message);
}
else if (message is AnotherMessage) {
// ...
}
});
In both room.OnMessage
functions there actually is a "type" (in the sense of "messageId") as first parameter.
In general I am missing TypeScript examples in the client documentation. In the server documentation there are plenty of those.
This being said the whole documentation is quite good in my opinion. Nevertheless there are some details which could be clearer, for example those mentioned here.