socket.io-client-csharp
socket.io-client-csharp copied to clipboard
Error on using GetComponent or set PlayerPrefs
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.
Sorry, I don’t know about Unity. According to the error message you provided, I can’t find anything about SocketIO.
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?
2 ways:
1. Use Fiddler to capture WebSocket traffic

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);
}
}

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.