forked from haneytron/simplsockets
-
Notifications
You must be signed in to change notification settings - Fork 2
Asynchronous usage with async and await
Thijs Elenbaas edited this page Jan 6, 2019
·
3 revisions
Much of the functionality of SimplSockets and SimplMessage is near instantaneous and in these cases asynchronous usage does not bring much benefit, but in some cases the application will need to wait, e.g. on establishing the connection or waiting for a reply. In these cases the libraries supply an Async variant of the functions.
You can await a connection having been made from both the server and client:
var connectedClient = await _server.WaitForNewClientAsync();
await _client.WaitForConnectionAsync();
You can await a reply from the server for SimplSockets and SimplMessage.
var receivedMessage = await client.SendReceiveAsync(messageToSend);
var receivedObject = await _client.SendReceiveAsync<ClassA, ClassA>(objectToSend);
you can also include a timeout (in ms) after which the function will stop waiting for a reply
var receivedObject = await _client.SendReceiveAsync<ClassA, ClassA>(objectToSend,1000);
These features are shown in more detail in this example