heroiclabs / nakama-unity

Unity client for Nakama server.
https://heroiclabs.com/docs/unity-client-guide
Other
411 stars 75 forks source link

Document that socket event handlers execute on the socket thread. #80

Closed novabyte closed 3 years ago

novabyte commented 4 years ago

We should document in the README that the socket event handlers execute on the socket thread rather than the Unity main thread. For when the code invoked in the event handler interacts with the UnityEngine functions (which are not thread-safe) a library like UnityMainThreadDispatcher should be used:

https://github.com/PimDeWitte/UnityMainThreadDispatcher

lugehorsam commented 4 years ago

C# has a SynchronizationContext.Current property that you can use to forward socket messages from another thread into the main thread. Using that, we could give users the option to either keep the messages on their own thread or even default them to the main thread (for new projects only) for ease-of-use. That could be configured with an enum or the like.

novabyte commented 4 years ago

@lugehorsam We should be careful with the synchronization context as it can cause severe performance problems with socket throughput onto the Unity main thread.