Reputation: 109
I'm using the following code to receive the frame string.
private SubscriberSocket subSocket;
void Start()
{
subSocket = new SubscriberSocket();
subSocket.Connect("tcp://localhost:5555");
}
void Update()
{
string data = subSocket.ReceiveFrameString();
}
The problem with this code is that the unity application freezes when no data is being received from the client. How can I stop the application from freezing when there isn't any data being received?
Upvotes: 1
Views: 405
Reputation: 36341
Methods like ReceiveFrameString
will block indefinitely until a message is received. There are a few ways to handle this, and it may depend on the library you are using. Note that ZeroMq is a protocol, and the libraries for .net I know about are clrzmq and NetMq.
Do the call on a background thread. Launch a longRunning task that only receives messages. Once received you can deal with them on the worker thread, start another task, or put them on a queue for the main thread to deal with. I'm not 100% how the threading model in unity works.
Add a timeout to the call. There should be an overload that takes a value for how long to wait. You should be able to give a low value, or zero to not wait. Be aware that the number of messages that are buffered will probably be limited.
Use async calls. At least libraries like NetMq have async receive calls that should be possible to use to avoid blocking.
Upvotes: 1