Skip to content
Thijs Elenbaas edited this page Jan 5, 2019 · 5 revisions

SimpleSockets and SimplMessage can be used in Unity. There is .net standard/.net desktop compatible implementation as well as a UWP implementation, which means that the library can be used both for desktop applications and devices like the Microsoft Hololens.

Processing callbacks in Update()

Normally SimplMessage will process each received message directly on a separate thread. However, In many Unity applications you might want to act upon received data in the Unity Update() function. SimplMessage supports this use case with a special callback:

client.AddUpdateCallBack<ClassA>((receivedMessage) =>
{
    // get data from received message cast to ClassA
    var receivedObject = receivedMessage.GetContent<ClassA>();
});

All incoming messages for this callback will be queued and be processed on the main thread when the following function is called:

client.UpdateCallbacks();

Because you may need to maintain a certain framerate, you can supply a maximum time in which messages can be processed:

client.UpdateCallbacks(TimeSpan.FromMilliseconds(10));

Singleton support

Because of the way the Unity scripting architecture is set up, singletons are used quite often. While one can argue if it the best way to do things, SimplSockets and SimplMessage support this by providing thread-safe singleton implementations for client and server, e.g.:

_client = SimplMessageClient.Instance;
Clone this wiki locally