socket.io-client-csharp icon indicating copy to clipboard operation
socket.io-client-csharp copied to clipboard

Error on using GetComponent or set PlayerPrefs

Open Megamil opened this issue 4 years ago • 4 comments

UnityEngine.UnityException: GetComponentFastPath can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

Megamil avatar Apr 07 '21 18:04 Megamil

Sorry, I don’t know about Unity. According to the error message you provided, I can’t find anything about SocketIO.

doghappy avatar Apr 08 '21 01:04 doghappy

I solved this problem, in fact it was the thread I was using, only the main thread can change components on the screen, and the iO socket created another thread.

I was able to solve it using the UnityThread class that was commented on in this forum: https://stackoverflow.com/questions/41330771/use-unity-api-from-another-thread-or-call-a-function-in-the-main-thread

but now my problem is when I Buid on the iPhone, it does not connect and does not display an error, how can I create some connection log?

Megamil avatar Apr 09 '21 16:04 Megamil

2 ways:

1. Use Fiddler to capture WebSocket traffic

image

2. Add Listener

You need to compile the Debug version of SocketIOClient, and use the compiled SocketIOClient in the Unity project

public class NewBehaviourScript : MonoBehaviour
{
    SocketIO socket = null;

    async void Start()
    {
        System.Diagnostics.Trace.Listeners.Add(new DebugListener());
        Debug.Log("Start");
        socket = new SocketIO(new Uri("http://localhost:11000"), new SocketIOOptions
        {
            Query = new Dictionary<string, string>
            {
                {"token", "io" }
            }
        });

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

        await socket.ConnectAsync();
        Debug.Log("End");
    }

    // Update is called once per frame
    void Update()
    {

    }
}

public class DebugListener : System.Diagnostics.TraceListener
{
    public override void Write(string message)
    {
        Debug.Log(message);
    }

    public override void WriteLine(string message)
    {
        Debug.Log(message);
    }
}

image

doghappy avatar Apr 12 '21 02:04 doghappy

Thanks, I'll test it yet, I haven't seen the answer before. But the last time I tried to solve it I found it to be something related to the 64bit of the mobile, before it worked on Android and not on iOS, I sent it to Playstore since Android was ok, then the Google store told me it was a 32bit binary and I had to upgrade to 64bits, then it stopped working.

Megamil avatar Apr 23 '21 23:04 Megamil