itisnajim / SocketIOUnity

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

Nothing really works when callback is called. #27

Closed cheneliezer closed 2 years ago

cheneliezer commented 2 years ago
        using System;

using System.Collections.Generic; using SocketIOClient; using SocketIOClient.Newtonsoft.Json; using UnityEngine; using UnityEngine.UI; using Newtonsoft.Json.Linq; using UnityEngine.SceneManagement;

public class ClientSocketManager : MonoBehaviour { public SocketIOUnity socket;

public Text ReceivedText;
public Image t;

// Start is called before the first frame update
void Start()
{
    //TODO: check the Uri if Valid.
    var uri = new Uri("http://localhost:3000");
    socket = new SocketIOUnity(uri, new SocketIOOptions
    {
        Query = new Dictionary<string, string>
            {
                {"token", "UNITY" }
            }
        ,
        EIO = 4
        ,
        Transport = SocketIOClient.Transport.TransportProtocol.WebSocket
    }); 
    socket.JsonSerializer = new NewtonsoftJsonSerializer();

    ///// reserved socketio events
    socket.OnConnected += (sender, e) =>
    {
                Debug.Log("connected");
        SceneManager.LoadScene("GameScene");
    };
    socket.Connect();

}

} this is my code. very simple, LoadScene does not work even do the callback is called ( debug.log connected works )

itisnajim commented 2 years ago

see: https://github.com/itisnajim/SocketIOUnity#receiving

UnityThread.executeInUpdate(() => {
    // anything related to unity thread run it here!
    objectToSpin.transform.Rotate(0, 45, 0);
});

so your code should be:

    socket.OnConnected += (sender, e) =>
    {
                Debug.Log("connected");
                UnityThread.executeInUpdate(() => {
                        SceneManager.LoadScene("GameScene");
                });
    };
cheneliezer commented 2 years ago

thank you ! ( also socket.OnUnityThread worked ! )