mikerochip / unity-websocket

Easy-to-use WebSocket MonoBehaviour for Unity. NativeWebSocket alternative.
Other
54 stars 7 forks source link

OnMessageReceived debouncing #10

Closed edstef closed 11 months ago

edstef commented 11 months ago

Hi, I just integrated this library switching over from https://github.com/endel/NativeWebSocket

I'm having issues using the event based approach you outline in the samples:

public class networking : MonoBehaviour
{

    public WebSocketConnection wsConnection;

...
void Awake()
    {
        DontDestroyOnLoad(gameObject);

        Connect();

        wsConnection.StateChanged += OnStateChanged;
        wsConnection.ErrorMessageReceived += OnErrorMessageReceived;
        wsConnection.MessageReceived += OnMessageReceived;
    }
...
 private void OnMessageReceived(WebSocketConnection connection, WebSocketMessage message)
    {
        Debug.Log("test")

I am seeing "test" printed upwards of 13 times for every socket message I send from the server. Do I need to debounce the message and if so is there a recommended approach?

mikerochip commented 11 months ago

The OnMessageReceived handler was changed back in July to not require you to do anything to remove the message from an internal list https://github.com/mikerochip/unity-websocket/commit/ecf87e8b19b5a2ee2efb7810221f6491efffa398

If you're on an old version you can click the update button in Unity's package manager.

If you're on the latest already then there's probably a bug in your client or server code and it would help to post both.

mikerochip commented 11 months ago

Feel free to re-open if you find an issue after testing. Make sure to post your client and server code if you do. Please and thank you!

edstef commented 11 months ago

Sorry for delayed response.. The issue was with server code: I had an issue where the socket connection was not clearing from cache and so the player was mapped to the same socket connection multiple times. Thanks for the response :D