doghappy / socket.io-client-csharp

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

Android Unity3D build: Socket IO callbacks are not executed #371

Closed brako closed 3 months ago

brako commented 3 months ago

Hello,

I am using the 3.0.6 version in my Unity3D project and everything is working fine so far. But I wanted to use the latest version 3.1.1. When I run my app the Unity editor with version 3.1.1, the code is running as expected. But if I compile the same code to an Android application, it is not working at all.

There is no error in the logs, it seems that the callbacks are not called.

I tried to compile SocketIO to .Net 4.8 and netstandard 2, But it doesn't change anything on the issue.

using UnityEngine;
using SocketIO.Client;
using SocketIO.Client.Transport;

public class SocketIOTest : MonoBehaviour
{
    private SocketIOClient client;

    async void Start()
    {
        this.client = new SocketIOClient("http://localhost:3000/", new SocketIOOptions
        {
            Reconnection = true,
            Transport = TransportProtocol.WebSocket
        });

        this.client.OnConnected += (sender, e) =>
        {
            Debug.Log("connected");
        };

        this.client.OnDisconnected += (sender, e) =>
        {
            Debug.Log("disconnected");
        };

        this.client.On("message", response =>
        {
            Debug.Log("receive message");
        });

        await this.client.ConnectAsync();
    }
}

Any help is appreciated Thanks

doghappy commented 3 months ago

you set Reconnection = true that means once connect fail it will retry in background. if you change it to false, you will be seeing the exception.

brako commented 3 months ago

Yes you are right! I can see the exception now.

SocketIO.Client.ConnectionException: Cannot connect to server 'http://10.42.0.33:3000/' 
---> SocketIO.Client.Transport.TransportException: Could not connect to 'ws://10.42.0.33:3000/socket.io/?EIO=4&transport=websocket' 
---> System.Net.WebSockets.WebSocketException: Unable to connect to the remote server 
---> System.Net.Sockets.SocketException: Access denied
    at System.Net.Sockets.Socket..ctor (System.Net.Sockets.AddressFamily addressFamily, System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType) [0x00068] in <5fe808a9ce234d7cb9b1a4e14f988a80>:0 
    at System.Net.WebSockets.WebSocketHandle.ConnectSocketAsync (System.String host, System.Int32 port, System.Threading.CancellationToken cancellationToken) [0x000b3] in <5fe808a9ce234d7cb9b1a4e14f988a80>:0 
    at System.Net.WebSockets.WebSocketHandle.ConnectAsyncCore (System.Uri uri, System.Threading.CancellationToken cancellationToken, System.Net.WebSockets.ClientWebSocketOptions options) [0x000d7] in <5fe808a9ce234d7cb9b1a4e14f988a8

Unity Android build settings property Internet Access was set to auto and it doesn't allow the app to use internet for some reason. I changed the value to required and this is now working just fine. This is a rookie mistake.

Thanks a lot for your quick response.