async void dealerListener() { AsyncIO.ForceDotNet.Force(); var socket = new DealerSocket(); socket.Options.Identity = Encoding.UTF8.GetBytes("client_" + System.Guid.NewGuid().ToString()); socket.Connect("tcp://45.56.102.215:5580"); try { var workerIsStarted = false; while (dealerIsStarted) { // Wait for worker while (!workerIsStarted) { socket.SendFrame("Client Ready"); string m; if (socket.TryReceiveFrameString(TimeSpan.FromSeconds(3), out m)) { string[] parts = m.Split(' '); Debug.Log("Delay: " + (CurrentUNIXTimeStampMilliseconds() - Convert.ToInt64(parts[0]))); m = string.Join(" ", parts.Skip(1)); if (string.Equals(m, "Worker Ready")) { Debug.Log("Worker Ready"); workerIsStarted = true; socket.SendFrame("Xmas Lights!"); } } } string msg; if (socket.TryReceiveFrameString(TimeSpan.FromSeconds(3), out msg)) { Debug.Log("Received: " + msg); string[] parts = msg.Split(' '); Debug.Log("Delay: " + (CurrentUNIXTimeStampMilliseconds() - Convert.ToInt64(parts[0]))); colorStr = parts[1]; socket.SendFrame("Will change color to " + msg); } await Task.Delay(500); } } finally { if (socket != null) { colorStr = "black"; socket.SendFrame("Client Stopping"); ((IDisposable)socket).Dispose(); } } }
public IEnumerable <DeviceStatus> GetDevices() { var reqSock = new DealerSocket(">inproc://queryRouter"); reqSock.SendReady += (o, e) => { e.Socket.SendFrame("SendStatus"); }; reqSock.Poll(); string msg; var results = new List <DeviceStatus>(); while (reqSock.TryReceiveFrameString(TimeSpan.FromSeconds(1), out msg)) { results.Add(JsonConvert.DeserializeObject <DeviceStatus>(msg)); } return(results); }
/// <summary> /// Tries to connect to the QDMS server. /// </summary> public void Connect() { if (Connected) { return; } lock (_realTimeRequestSocketLock) { _realTimeRequestSocket = new DealerSocket(_realTimeRequestConnectionString); _realTimeRequestSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); // Start off by sending a ping to make sure everything is regular var reply = string.Empty; try { _realTimeRequestSocket.SendMoreFrame(string.Empty).SendFrame(MessageType.Ping); if (_realTimeRequestSocket.TryReceiveFrameString(TimeSpan.FromSeconds(1), out reply)) { _realTimeRequestSocket.TryReceiveFrameString(TimeSpan.FromMilliseconds(50), out reply); } } catch { Disconnect(); } if (reply == null || !reply.Equals(MessageType.Pong, StringComparison.InvariantCultureIgnoreCase)) { try { _realTimeRequestSocket.Disconnect(_realTimeRequestConnectionString); } finally { _realTimeRequestSocket.Close(); _realTimeRequestSocket = null; } RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server.")); return; } _realTimeRequestSocket.ReceiveReady += RealTimeRequestSocketReceiveReady; } lock (_realTimeDataSocketLock) { _realTimeDataSocket = new SubscriberSocket(_realTimeDataConnectionString); _realTimeDataSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); _realTimeDataSocket.ReceiveReady += RealTimeDataSocketReceiveReady; } lock (_historicalDataSocketLock) { _historicalDataSocket = new DealerSocket(_historicalDataConnectionString); _historicalDataSocket.Options.Identity = Encoding.UTF8.GetBytes(_name); _historicalDataSocket.ReceiveReady += HistoricalDataSocketReceiveReady; } _lastHeartBeat = DateTime.Now; _heartBeatTimer = new NetMQTimer(TimeSpan.FromSeconds(HeartBeatPeriodInSeconds)); _heartBeatTimer.Elapsed += HeartBeatTimerElapsed; _historicalDataTimer = new NetMQTimer(TimeSpan.FromSeconds(HistoricalDataRequestsPeriodInSeconds)); _historicalDataTimer.Elapsed += HistoricalDataTimerElapsed; _poller = new NetMQPoller { _realTimeRequestSocket, _realTimeDataSocket, _historicalDataSocket, _heartBeatTimer, _historicalDataTimer }; _poller.RunAsync(); }