itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
379 stars 65 forks source link

onError not called for error return from the server #56

Closed royibernthal closed 1 year ago

royibernthal commented 1 year ago

When emitting the "message" event, I'm throwing an exception on the server on purpose. I'm expecting OnError to trigger, but it doesn't.

This is the exception packet I captured from the server, which doesn't trigger OnError on the client.

42["exception",{"status":"error","message":"test"}]

using UnityEngine;
using System;

public class Test : MonoBehaviour
{
    void Start() {
        var uri = new Uri("ws://localhost:8090");

        var socket = new SocketIOUnity(uri);

        socket.OnConnected += (sender, e) =>
        {
            Debug.Log("OnConnected");

            socket.Emit("message", "bla"); // throwing an exception on the server on purpose
        };

        socket.OnError += (sender, e) => {
            Debug.Log("OnError: " + e); // should be triggered with exception from the server, but never triggered
        };

        socket.Connect();
    }
}
itisnajim commented 1 year ago

replace ws: by http: and set the transporter to WebSocket like in:

var uri = new Uri("http://localhost:8090");
socket = new SocketIOUnity(uri, new SocketIOOptions
{
    Query = new Dictionary<string, string>
        {
            //{"token", "UNITY" }
        }
    ,
    Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
});

add the other callbacks so you know in which phase the socket get disconnected:

        socket.OnError += (sender, e) => {
            Debug.Log("OnError: " + e);
        };

        socket.OnPing += (sender, e) =>
        {
            Debug.Print("Ping");
        };
        socket.OnPong += (sender, e) =>
        {
            Debug.Print("Pong: " + e.TotalMilliseconds);
        };
        socket.OnDisconnected += (sender, e) =>
        {
            Debug.Print("disconnect: " + e);
        };
        socket.OnReconnectAttempt += (sender, e) =>
        {
            Debug.Print($"{DateTime.Now} Reconnecting: attempt = {e}");
        };
royibernthal commented 1 year ago

I did everything you said, none of the callbacks were called except for the usual ping pong.

royibernthal commented 1 year ago

It seems socket.On("exception"...) works.