colyseus-unity-sdk icon indicating copy to clipboard operation
colyseus-unity-sdk copied to clipboard

Cannot overwrite message callback

Open sticmac opened this issue 2 years ago • 2 comments

When using ColyseusRoom.OnMessage twice on the same message ID, I get the following exception:

ArgumentException: An item with the same key has already been added. Key: myMessage

An easy fix would be to modify the OnMessage method to take in consideration if a previous message with the given id already exists. However, I wanted to know if this is a bug or a real design choice before submitting a PR.

sticmac avatar Jul 06 '22 22:07 sticmac

Thanks for reporting, @sticmac,

We've added support for multiple callbacks on the other clients for upcoming version 0.15 but it turns out the Unity SDK is still missing this feature (on https://github.com/colyseus/colyseus-unity-sdk/pull/177)

Ideally, the Unity SDK should be able to attach and detach multiple callbacks, for example:

var detachCallback1 = room.OnMessage("xx", () => { /* first */ });
var detachCallback2 = room.OnMessage("xx", () => { /* second */ });

// detaching the "OnMessage" callback:
detachCallback1();
detachCallback2();

endel avatar Jul 07 '22 13:07 endel

I made a pull request (#199) containing a solution like the example you provide :)

sticmac avatar Jul 11 '22 15:07 sticmac