room.OnMessage not registered for Schema message: MyMessage

I want to send a Broadcast message to unity client, but I have this error, any Idea:

in my Colyseus File Room I have:
onCreate(options: any) {
// configuring room

this.setMetadata({name: options.roomName});
this.setPatchRate(1000 / 20);
// Setting inital state
const state: State = new State();
this.setState(state);
// Listening to client's messages
this.onMessage("initGame", (client, message) => {
  const data = new MyMessage();
  data.message = "readyToStart";
  this.broadcast(data);
});

on unity client:

room.OnMessage<MyMessage>("readyToStart", (message) => {
Debug.Log("message received from server");
Debug.Log(message);
menu.EnableButtonStart();
});

The message itself contains only data so it isn’t used to detect the message type (you’re trying to catch “readyToStart” messages, right?).

I don’t know how Unity client works but as I could understand it separates messages by classes names if not specified. So maybe this should look like this:

room.OnMessage<MyMessage>("MyMessage", (message) => {
  // your code
});

If you're sending Schema messages, you should listen for them in the client-side like this @lucks (the documentation is not doing a good job on the C# section)

room.OnMessage((MyMessage message) =>
{
	Debug.Log("Received Schema message:");
	Debug.Log(message.message);
});

(See demo unity3d source code here: https://github.com/colyseus/colyseus-unity3d/blob/30c9b7d8e8259a7516fafcbd26179a0ab6a91ec9/Assets/ColyseusClient.cs#L157-L161)

You may also declare interfaces that contains Message to get them generated to C# through schema-codegen (instead of using Schema for strictly typing messages)

Example:

interface ReadyToStartMessage {
  data: string;
}

// ...

this.broadcast("readyToStart", { data: "hello!" })

Client-side (C#, after running schema-codegen)

room.OnMessage<ReadyToStartMessage>("readyToStart", (message) => {
  // message.data
});

Hope this helps. I'm taking notes to update the C# documentation as it doesn't seem to be very helpful atm

@endel said in room.OnMessage not registered for Schema message: MyMessage:

room.OnMessage((MyMessage message) =>
{
Debug.Log("Received Schema message:");
Debug.Log(message.message);
});

Works perfect, thanks, yes I was reading the documentation, but not well at all.