void t_Elapsed() { NetMQContext ctx = NetMQContext.Create(); NetMQSocket socket = ctx.CreateSubscriberSocket(); // TODO: change socket connection ip, port socket.Connect("tcp://172.16.12.188:5555"); socket.Subscribe(_tagID.ToString("000")); socket.ReceiveReady += (s, a) => { string topic = socket.ReceiveString(); string message = socket.ReceiveString(); msg m = JsonConvert.DeserializeObject <msg>(message); WriteText(m); //Application.DoEvents(); }; Poller poller = new Poller(); poller.AddSocket(socket); poller.Start(); }
protected override void StartMethod() { _zmqContext = NetMQContext.Create(); _zmqSocket = _zmqContext.CreateSubscriberSocket(); if (string.IsNullOrEmpty(_subsPrefix)) { _zmqSocket.Subscribe(string.Empty); } else { _zmqSocket.Subscribe(Encoding.UTF8.GetBytes(_subsPrefix)); } _zmqSocket.Connect(_endPoint); }
public MessageHelper(string address, string topic) { _context = NetMQContext.Create(); _subscribeSocket = _context.CreateSubscriberSocket(); _subscribeSocket.Connect(address); _subscribeSocket.ReceiveReady += SubscribeSocketOnReceiveReady; _subscribeSocket.Subscribe(topic); _poller = new Poller(); _poller.AddSocket(_subscribeSocket); Task.Factory.StartNew(_poller.Start); }
public void Start() { _zmqContext = NetMQContext.Create(); _zmqSocket = _zmqContext.CreateSubscriberSocket(); if (string.IsNullOrEmpty(_subsPrefix)) { _zmqSocket.Subscribe(string.Empty); } else { _zmqSocket.Subscribe(Encoding.UTF8.GetBytes(_subsPrefix)); } _zmqSocket.Connect(_endPoint); _zmqSocket.ReceiveReady += _zmqSocket_ReceiveReady; _poller = new Poller(); _poller.AddSocket(_zmqSocket); Task.Factory.StartNew(() => Run(), TaskCreationOptions.LongRunning); }
// initialized sprites void Start() { AsyncIO.ForceDotNet.Force(); calImages = new Sprite[10]; calImages[0] = Resources.Load <Sprite>("notargets"); calImages[1] = Resources.Load <Sprite>("targetTL"); calImages[2] = Resources.Load <Sprite>("targetTM"); calImages[3] = Resources.Load <Sprite>("targetTR"); calImages[4] = Resources.Load <Sprite>("targetML"); calImages[5] = Resources.Load <Sprite>("targetMM"); calImages[6] = Resources.Load <Sprite>("targetMR"); calImages[7] = Resources.Load <Sprite>("targetBL"); calImages[8] = Resources.Load <Sprite>("targetBM"); calImages[9] = Resources.Load <Sprite>("targetBR"); calCount = 0; curCount = 0; gameObject.GetComponent <Image> ().sprite = calImages [calCount]; //max coords 1180*564 transX = 0; transY = 0; targetPrefab = GameObject.FindGameObjectWithTag("RaycastTarget"); targetPrefab.SetActive(false); targetPrefab.layer = 2; //ignore raycast layer isVisible = false; AsyncIO.ForceDotNet.Force(); //setup sockets //hangs???? //http://forum.unity3d.com/threads/netmq-basic.298104/ //must compile myself //https://github.com/zeromq/netmq/issues/98 context = NetMQContext.Create(); server = context.CreatePublisherSocket(); server.Bind("tcp://127.0.0.1:5556"); client = context.CreateSubscriberSocket(); client.Connect("tcp://127.0.0.1:5556"); client.Subscribe("coord"); Debug.Log(System.Environment.Version); /*server.SendMore("coord").Send ("200 200"); * string top = client.ReceiveString (); * string message = client.ReceiveString (); * Debug.Log (message); * string[] coord = message.Split (); * transX = int.Parse (coord [0]); * transY = int.Parse (coord [1]);*/ }
// initialized sprites void Start() { AsyncIO.ForceDotNet.Force(); calImages = new Sprite[10]; calImages[0] = Resources.Load<Sprite>("notargets"); calImages[1] = Resources.Load<Sprite>("targetTL"); calImages[2] = Resources.Load<Sprite>("targetTM"); calImages[3] = Resources.Load<Sprite>("targetTR"); calImages[4] = Resources.Load<Sprite>("targetML"); calImages[5] = Resources.Load<Sprite>("targetMM"); calImages[6] = Resources.Load<Sprite>("targetMR"); calImages[7] = Resources.Load<Sprite>("targetBL"); calImages[8] = Resources.Load<Sprite>("targetBM"); calImages[9] = Resources.Load<Sprite>("targetBR"); calCount = 0; curCount = 0; gameObject.GetComponent<Image> ().sprite = calImages [calCount]; //max coords 1180*564 transX = 0; transY = 0; targetPrefab = GameObject.FindGameObjectWithTag ("RaycastTarget"); targetPrefab.SetActive (false); targetPrefab.layer = 2;//ignore raycast layer isVisible = false; AsyncIO.ForceDotNet.Force(); //setup sockets //hangs???? //http://forum.unity3d.com/threads/netmq-basic.298104/ //must compile myself //https://github.com/zeromq/netmq/issues/98 context = NetMQContext.Create (); server = context.CreatePublisherSocket (); server.Bind("tcp://127.0.0.1:5556"); client = context.CreateSubscriberSocket (); client.Connect("tcp://127.0.0.1:5556"); client.Subscribe ("coord"); Debug.Log (System.Environment.Version); /*server.SendMore("coord").Send ("200 200"); string top = client.ReceiveString (); string message = client.ReceiveString (); Debug.Log (message); string[] coord = message.Split (); transX = int.Parse (coord [0]); transY = int.Parse (coord [1]);*/ }
public AsyncRequestSocket(NetMQContext context, string address, Func <TRequest, byte[]> requestMarshaller, Func <byte[], TReply> replyUnmarshaller, IFiber fiber) { _requestMarshaller = requestMarshaller; _replyUnmarshaller = replyUnmarshaller; _fiber = fiber; _internalChannel.SetRequestHandler(_fiber, OnRequest); _replyContext = context; int basePort = int.Parse(address.Split(':')[2]); _replySocket = _replyContext.CreateSocket(ZmqSocketType.Sub); _replySocket.Connect(address.Substring(0, address.LastIndexOf(":")) + ":" + (basePort + 1)); _replySocket.Subscribe(_id); _requestSocket = _replyContext.CreateSocket(ZmqSocketType.Push); _requestSocket.Connect(address); _fiber.Start(); Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning); }
public void MultipleLargeMessages() { byte[] largeMessage = new byte[12000]; for (int i = 0; i < 12000; i++) { largeMessage[i] = (byte)(i % 256); } using (NetMQContext context = NetMQContext.Create()) { using (NetMQSocket pubSocket = context.CreatePublisherSocket()) { pubSocket.Bind("tcp://127.0.0.1:5558"); using (NetMQSocket subSocket = context.CreateSubscriberSocket()) { subSocket.Connect("tcp://127.0.0.1:5558"); subSocket.Subscribe(""); Thread.Sleep(1000); pubSocket.Send(""); subSocket.Receive(); for (int i = 0; i < 100; i++) { pubSocket.Send(largeMessage); byte[] recvMesage = subSocket.Receive(); for (int j = 0; j < 12000; j++) { Assert.AreEqual(largeMessage[j], recvMesage[j]); } } } } } }
public void Execute(NetMQSocket socket) { #pragma warning disable 618 socket.Subscribe(_topic); #pragma warning restore 618 }
private void ReceiveEvents(String subscriptionUri, String topic, CancellationToken cancellationToken) { using (NetMQContext context = NetMQContext.Create()) { using (NetMQSocket subscriberSocket = context.CreateSubscriberSocket()) { subscriberSocket.IgnoreErrors = true; subscriberSocket.Connect(subscriptionUri); subscriberSocket.Subscribe(topic); Logger.Info("ZeroMQCache: Connected to subscriptionUri \"{0}\".", subscriptionUri); // Eventhandler delegate to handle receiving messages subscriberSocket.ReceiveReady += (sender, args) => { try { if (args.ReceiveReady) { // Recieve and relay NetMQMessage netMQmessage = args.Socket.ReceiveMessage(); // Recieve the message ZeroMQMessage message = new ZeroMQMessage() { Topic = netMQmessage.Pop().ConvertToString(), Client = netMQmessage.Pop().ConvertToString(), Content = netMQmessage.Pop().ConvertToString() }; Logger.Debug("ZeroMQCache: Received -> {0}", message.ToString()); XmlCacheEvent cacheEvent = XmlCacheEvent.FromXml(message.Content); if (cacheEvent != null) { OnCacheEvent(cacheEvent.CacheRegion, cacheEvent.Key, cacheEvent.EventType); } } } catch (OperationCanceledException) { // We have been asked to cancel operation return; } catch (Exception ex) { Logger.Error("ZeroMQCache: Error receiving message.", ex); } }; while (!cancellationToken.IsCancellationRequested) { try { subscriberSocket.Poll(TimeSpan.FromSeconds(1)); } catch (OperationCanceledException) { // We have been asked to cancel operation break; } catch (Exception ex) { Logger.Error("ZeroMQCache: Error polling for messages.", ex); } } // Close socket subscriberSocket.Close(); } context.Terminate(); } }