SocketIOUnity icon indicating copy to clipboard operation
SocketIOUnity copied to clipboard

Not connect to socket io server on android

Open DamaSistem opened this issue 3 years ago • 15 comments

Hi there, I couldn't get it to connect on the Android phone. It works fine in Unity editor. Scripting backend IL2CPP api Compatibility level .Net Framework. I also use server-side codes in another react native application. I don't think there is a problem on the server side. Socket versions Unity side v1.0.0 , server side v4.1.3. I opened a project from scratch and pulled the unity side to v1.1.3 and the server side to v4.5.4, but it still didn't work. I would be glad if you can help.

DamaSistem avatar Nov 23 '22 06:11 DamaSistem

try upgrading unity; see https://github.com/itisnajim/SocketIOUnity/issues/36

itisnajim avatar Nov 29 '22 11:11 itisnajim

@itisnajim I am using the unity 2021.3.30f1 LTS version but I am not able to connect to the server on mobile devices, it's connecting fine with the editor.

Any help is highly appreciated.

devalvitec avatar Sep 25 '23 14:09 devalvitec

try these: https://github.com/itisnajim/SocketIOUnity/issues/60

itisnajim avatar Sep 27 '23 10:09 itisnajim

it's not connecting with il2cpp in unity android

Atul2011421 avatar Sep 30 '23 09:09 Atul2011421

Anyone found a fix for this? Cant get it to work on android on 2021.3.8f1

ScottasM avatar Oct 30 '23 19:10 ScottasM

@ScottasM update your Unity to 2022.3.10f1 or Change and try between the Faster (smaller) builds option/ Faster runtime option at player setting for Android. Faster runtime option might work.

devalvitec avatar Oct 31 '23 03:10 devalvitec

@devalvitec Nope, still not working. Updated the unity version, tried changing the build options.

ScottasM avatar Oct 31 '23 13:10 ScottasM

Im also building with il2cpp if that matters

ScottasM avatar Oct 31 '23 17:10 ScottasM

what are the error exactly you are getting ?

devalvitec avatar Nov 01 '23 05:11 devalvitec

@devalvitec Im getting no errors, nothing happens. Not even the OnError gets called.

ScottasM avatar Nov 01 '23 09:11 ScottasM

Okay, after 4 days of this torture i fixed it. for any other lost souls out there here what was wrong : Apparently android didnt trust my ssl certificate, and didnt print anything to logcat, so when i added all the events like OnReconnectFail, OnRecconectAttempt and etc. i finally saw the error.

what i had to do was : cat certificate.crt ca_bundle.crt > chained.pem to generate the chained.pem file for android to trust

and then in my flask socketio app i changed the path to point from certificate.crt to chained.pem

ScottasM avatar Nov 03 '23 14:11 ScottasM

@ScottasM
I've deployed my solution on cloud run so your solution can't be applied The library must provide a way to set supported TLS encyrption like this https://stackoverflow.com/questions/68268113/how-can-unity-connect-with-secure-websocket-based-on-node-js/68280594#68280594

joyscoreinc avatar Nov 19 '23 05:11 joyscoreinc

@ScottasM

I've deployed my solution on cloud run so your solution can't be applied

The library must provide a way to set supported TLS encyrption like this

https://stackoverflow.com/questions/68268113/how-can-unity-connect-with-secure-websocket-based-on-node-js/68280594#68280594

Try this if solve your issue:

https://github.com/doghappy/socket.io-client-csharp/issues/94

itisnajim avatar Nov 19 '23 12:11 itisnajim

hey guys i stuck on this issue too, on local work on unity editor and android. but when trying to connect to server nothing happens. i can connect to my remote server with my reactjs app normaly, just the unity no connect. Unity version: 2023.3.34f1 this is my code on Unity:

` socket = new SocketIOUnity(uri_url, new SocketIOOptions { EIO = 4 , Transport = SocketIOClient.Transport.TransportProtocol.Polling }) { JsonSerializer = new NewtonsoftJsonSerializer() };

    ///// reserved socketio events
    socket.OnConnected += (sender, e) =>
    {
        Debug.Log("socket.OnConnected");
    };
    socket.OnPing += (sender, e) =>
    {
        Debug.Log("Ping");
    };
    socket.OnPong += (sender, e) =>
    {
        Debug.Log("Pong: " + e.TotalMilliseconds);
    };
    socket.OnDisconnected += (sender, e) =>
    {
        Debug.Log("disconnect: " + e);
    };
    socket.OnReconnectAttempt += (sender, e) =>
    {
        Debug.Log($"OnReconnectAttempt: attempt = {e}");
    };
    socket.OnReconnectError += (sender, e) =>
    {
        Debug.Log($"OnReconnectError: attempt = {e}");
    };
    socket.OnReconnectFailed += (sender, e) =>
    {
        Debug.Log($"OnReconnectFailed: attempt = {e}");
    };
    socket.On("game", (response) =>
    {
        gameInfor = response.GetValue<WebsocketMessage>();
        if (gameInfor.eventType == "winner" && gameInfor.userId == userObject.userUid)
        {
            userObject.bolhas += gameInfor.amount;
            winnerColor = gameInfor.color;
            Debug.Log($"GANHOU {winnerColor} - {gameInfor.userId} - {gameInfor.eventType}");
        }
    });
    socket.ConnectAsync();
    `

i trying to use TransportProtocol.Polling and TransportProtocol.WebSocket but still same problem on my reactjs app i connect to my socket io server using https protocol. dont know what to do about this more

TiagoAndreAlmeida avatar Feb 15 '24 15:02 TiagoAndreAlmeida

i fix after doing this o SocketIOOptions, i set AutoUpgrade to false. now on my looks like this: socket = new SocketIOUnity(uri_url, new SocketIOOptions { AutoUpgrade = false, EIO = 4 , Transport = SocketIOClient.Transport.TransportProtocol.Polling }) { JsonSerializer = new NewtonsoftJsonSerializer() };

TiagoAndreAlmeida avatar Feb 15 '24 17:02 TiagoAndreAlmeida