Closed novabyte closed 3 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.
@lugehorsam We should be careful with the synchronization context as it can cause severe performance problems with socket throughput onto the Unity main thread.
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