Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AsyncIO;
- using NetMQ;
- using NetMQ.Sockets;
- using UnityEngine;
- using System;
- using System.Collections.Generic;
- using System.Threading;
- /// <summary>
- /// Example of requester who only sends Hello. Very nice guy.
- /// You can copy this class and modify Run() to suits your needs.
- /// To use this class, you just instantiate, call Start() when you want to start and Stop() when you want to stop.
- /// </summary>
- public class TestRequester : RunAbleThread {
- public event Action<bool> OnReceiveResponse;
- private Queue<string> outgoing;
- private Mutex queueMut;
- /// <summary>
- /// Request Hello message to server and receive message back. Do it 10 times.
- /// Stop requesting when Running=false.
- /// </summary>
- protected override void Run() {
- queueMut = new Mutex();
- ForceDotNet.Force(); // this line is needed to prevent unity freeze after one use, not sure why yet
- using (RequestSocket client = new RequestSocket()) {
- client.Connect("tcp://localhost:5555");
- while (Running) {
- queueMut.WaitOne();
- if (outgoing.Count > 0) {
- client.SendFrame(outgoing.Dequeue());
- string message = null;
- bool gotMessage = false;
- while (Running && !gotMessage) {
- gotMessage = client.TryReceiveFrameString(out message); // this returns true if it's successful
- }
- OnReceiveResponse(message == "ACK");
- } else {
- //TODO make it not do busy cycles somehow
- }
- queueMut.ReleaseMutex();
- }
- }
- NetMQConfig.Cleanup(); // this line is needed to prevent unity freeze after one use, not sure why yet
- }
- public void AddSendCommand(string msg) {
- queueMut.WaitOne();
- outgoing.Enqueue(msg);
- queueMut.ReleaseMutex();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement