public static ISubscriberSocket AsSubscriber(this EasyZMqConfigurer configurer, string topic) { var serializer = configurer.Serializer; var addressBinder = configurer.AddressBinder; var loggerFactory = configurer.LoggerFactory; var messageDispatcher = configurer.MessageDispatcher; var socket = new NetMQ.Sockets.SubscriberSocket(); socket.Subscribe(topic); return(new SubscriberSocket(serializer, addressBinder, loggerFactory, messageDispatcher, socket)); }
public void connect() { //string endpoint = "tcp://127.0.0.1:5561"; //using (NetMQContext context = NetMQContext.Create()) //using (NetMQ.Sockets.SubscriberSocket responder = context.CreateSubscriberSocket()) //{ // responder.Connect(endpoint); // responder.Subscribe("hello"); // while (true) // { // string msg = responder.ReceiveString(); // Console.WriteLine(msg); // Thread.Sleep(500); // } //} string name = AppDomain.CurrentDomain.FriendlyName; string endpoint = ConfigurationManager.AppSettings["pubAdress"]; //using (context = NetMQContext.Create()) using (responder = new NetMQ.Sockets.SubscriberSocket()) { responder.Connect(endpoint); responder.Subscribe(this.target); while (true) { NetMQMessage msgList = responder.ReceiveMultipartMessage(); if (msgList.Count() > 1) { var body = msgList[1]; byte[] data = body.ToByteArray(); MemoryStream ms = new MemoryStream(data); BinaryFormatter bf = new BinaryFormatter(); CmdMessage cmd = (CmdMessage)bf.Deserialize(ms); ms.Close(); if (this.onReceive != null) { this.onReceive(cmd); } } } } }
//! //! client function, listening for messages in receiveMessageQueue from server (executed in separate thread) //! public void listener() { //create NetMQ context NetMQContext ctx = NetMQContext.Create(); NetMQ.Sockets.SubscriberSocket receiver = ctx.CreateSubscriberSocket(); receiver.Subscribe("client"); receiver.Subscribe("ncam"); receiver.Subscribe("record"); receiver.Connect("tcp://" + VPETSettings.Instance.serverIP + ":5556"); lastReceiveTime = currentTimeTime; Debug.Log("Listener connected: " + "tcp://" + VPETSettings.Instance.serverIP + ":5556"); while (isRunning) { try { this.receiveMessageQueue.Add(receiver.ReceiveString(NetMQ.zmq.SendReceiveOptions.DontWait).Substring(7)); lastReceiveTime = currentTimeTime; // Debug.Log("Received sync message: " + receiveMessageQueue[receiveMessageQueue.Count-1]); } catch (AgainException ex) { listenerRestartCount = Mathf.Min(int.MaxValue, listenerRestartCount + 1); // VPETSettings.Instance.msg = string.Format("Exception in Listener: {0}", listenerRestartCount); if (currentTimeTime - lastReceiveTime > receiveTimeout) { // break; } } } receiver.Disconnect("tcp://" + VPETSettings.Instance.serverIP + ":5556"); receiver.Close(); }