doghappy / socket.io-client-csharp

socket.io-client implemention for .NET
MIT License
721 stars 124 forks source link

OnAny fires but not On("eventName", response=>{foo();}); any idea? #278

Closed ghost closed 2 years ago

ghost commented 2 years ago

Problem

The OnAny function works normally. The string value received through this function can be compared normally. However, it doesn't recognize eventName that I handed over through "On("eventName", response=> {foo();});".

OS / Platform

Windows / Unity 19.4.34f1, target .net 4.xx

Code

    void Awake()
    {
        client = new ClientInfo("null", userName);
        clientSocket = new SocketIO(serverIP);
    }

    async void Start()
    {
        //For Event Debug
        clientSocket.OnAny((eventName, data) =>
        {
            Debug.Log($"OnAny/{eventName}:{data}");
        });// It works

        clientSocket.On("[user] join", response =>
        {
            userList.Add(JsonConvert.DeserializeObject<ClientInfo>(response.GetValue<string>()));
            Debug.Log($"client Socket current Id : {clientSocket.Id}");
        });//not works

        clientSocket.On("[user] leave", response =>
        {
            string userSocketId = GetUserIdFromData(response.GetValue<string>());
            userList.RemoveWhere(info => info.id == userSocketId);
            Debug.Log($"Delete {userSocketId} from userList");
        });//not works

        clientSocket.On("connection", response =>
        {
            Debug.Log("connection");
        });// not works

        Connect();
    }
    async void Connect()
    {
        if (clientSocket == null)
            return;

        clientSocket.OnConnected += async (sender, e) =>
        {
            Debug.Log($"Emit Client name {client.name}");
            await clientSocket.EmitAsync("[user] join", client.name);
        };**//it works**

        Debug.Log($"Connect Async");
        await clientSocket.ConnectAsync();
    }

Etc

Unity console log image

doghappy commented 2 years ago

maybe an exception was thrown, have you tried to move Log to the first line?

        clientSocket.On("[user] join", response =>
        {
            Debug.Log($"client Socket current Id : {clientSocket.Id}");
            userList.Add(JsonConvert.DeserializeObject<ClientInfo>(response.GetValue<string>()));
        });//not works

maybe you can clean your code like this?

        clientSocket.On("[user] join", response =>
        {
            Debug.Log($"client Socket current Id : {clientSocket.Id}");
            userList.Add(response.GetValue<ClientInfo>());
        });
ghost commented 2 years ago

@doghappy Thanks to your reply For some reason, I confirmed that it works normally after the SocketIO .net update. When error occurred I use the OnAny like this haha

            Debug.Log($"OnAny/{eventName}:{data}");

            if(eventName == "[user] enter")
            {
                Debug.Log("User Enter");

Thanks @doghappy :) have a good day