// Update is called once per frame private void Update() { _timeSinceLastPub += Time.deltaTime; if (_timeSinceLastPub >= 1 / ZmqPubRate) { var pubMsg = new Msg(); var position = _hip.position; var velocity = _hip.velocity; var msgData = new MsgData { Position = new[] { position.x, position.y, position.z }, Velocity = new[] { velocity.x, velocity.y, velocity.z }, ID = id }; var pubMsgPayload = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msgData)); pubMsg.InitGC(pubMsgPayload, pubMsgPayload.Length); _pub.Send(ref pubMsg, false); _timeSinceLastPub = 0; } //string recvStr; if (_sub.TryReceiveFrameString(out recvStr)) { Debug.Log(recvStr); var recvJson = JsonConvert.DeserializeObject <Dictionary <string, string> >(recvStr); //if (int.Parse(recvJson["ID"]) != id) return; var forceX = float.Parse(recvJson["force_x"]); var forceY = float.Parse(recvJson["force_y"]); var forceZ = float.Parse(recvJson["force_z"]); _hip.AddRelativeForce(forceX, forceY, -(forceZ * 2)); //negative z because conversion from right-hand to left-hand coordinates } }
// Update is called once per frame private void Update() { _timeSinceLastPub += Time.deltaTime; if (_timeSinceLastPub >= 1 / ZmqPubRate) { var pubMsg = new Msg(); var position = _hip.position; var velocity = _hip.velocity; var msgData = new MsgData { Position = new[] { position.x, position.y, position.z }, Velocity = new[] { velocity.x, velocity.y, velocity.z }, ID = id }; var pubMsgPayload = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msgData)); pubMsg.InitGC(pubMsgPayload, pubMsgPayload.Length); _pub.Send(ref pubMsg, false); _timeSinceLastPub = 0; } string recvStr; if (_sub.TryReceiveFrameString(out recvStr)) { Debug.Log(recvStr); } }
public void Publish(string filter, PushMsg message) { if (_disposeCount != 0) { throw new ObjectDisposedException(this.GetType().Name); } try { _socket.Send(filter, false, true); _socket.Send(message.SerializeMessage()); } catch (TerminatingException) { Debug.WriteLine(string.Format("TerminatingException: auto-disposing {0}...", this.GetType().Name)); ((IDisposable)this).Dispose(); } }
/// <summary> /// Subscribe To Heartbeat /// </summary> public void subscribeToHeartbeat() { // Create the socket, connect to it, and subscribe to the topic - CheckHeartbeat var heartbeatSub = _context.CreateSubscriberSocket(); heartbeatSub.Connect(ConfigurationManager.AppSettings["heartbeatSubAddr"]); heartbeatSub.Subscribe(ConfigurationManager.AppSettings["checkHeartbeatTopic"]); while (true) { Console.WriteLine("REC: " + heartbeatSub.ReceiveString()); // Build and send the response to the check string message = String.Concat(ConfigurationManager.AppSettings["checkHeartbeatTopicResponse"], " <params>", ConfigurationManager.AppSettings["serviceName"], "</params>"); _publisher.Send(message); Console.WriteLine("PUB: " + message); } }
public bool sendMessage(PUBTYPE cmd) { if (type == NetworkType.SUB) { return(false); } if (publisher == null) { return(false); } try { if (typeof(PUBTYPE) == typeof(IRawData)) { Msg msg = new Msg(); IRawData d = (IRawData)(cmd); msg.InitGC(d.Data, d.Data.Length); Console.WriteLine(d.Data.Length); publisher.Send(ref msg, false); } else { //cmd is a command and we serialize it System.IO.MemoryStream ms = new System.IO.MemoryStream(); Serializer.Serialize <PUBTYPE>(ms, cmd); Msg msg = new Msg(); msg.InitGC(ms.ToArray(), (int)ms.Length); publisher.Send(ref msg, false); } } catch (Exception e) { string msg = e.Message; return(false); } return(true); }
public void Publish <T>(long eventCode, T message) where T : class { if (message == null) { throw new ArgumentNullException("message"); } const int int64Length = 8; var ms = new MemoryStream(); try { try { Serializer.Serialize <T>(ms, message); } catch (InvalidOperationException ex) { throw new SerializationException(ex, "Error when serializing event of type {0}. Check if that type is a valid Protobuf contract (annotated with ProtoContractAttribute).", typeof(T).FullName); } catch (Exception ex) { throw new SerializationException(ex, "Error when serializing event of type {0}.", typeof(T).FullName); } byte[] buffer = ms.ToArray(); _socket.Send(BitConverter.GetBytes(eventCode), int64Length, SendReceiveOptions.SendMore); _socket.Send(buffer, buffer.Length, SendReceiveOptions.None); if (_verboseLog) { _traces.Debug("Publisher({0:x}): event {1} sent.", _instanceHashCode, eventCode); } } catch (TerminatingException) { // We ignore errors if ZMQ context is in termination phase. } finally { ms.Dispose(); } }
static void Main(string[] args) { //using (var server = new ResponseSocket("@tcp://localhost:5556")) // bind //using (var client = new RequestSocket(">tcp://localhost:5556")) // connect //{ // // Send a message from the client socket // client.SendFrame("Hello"); // // Receive the message from the server socket // string m1 = server.ReceiveFrameString(); // Console.WriteLine("From Client: {0}", m1); // // Send a response back from the server // server.SendFrame("Hi Back"); // // Receive the response from the client socket // string m2 = client.ReceiveFrameString(); // Console.WriteLine("From Server: {0}", m2); //} const int MegaBit = 1024; const int MegaByte = 1024; using (var pub = new PublisherSocket()) using (var sub1 = new SubscriberSocket()) using (var sub2 = new SubscriberSocket()) { pub.Options.MulticastHops = 2; pub.Options.MulticastRate = 40 * MegaBit; // 40 megabit pub.Options.MulticastRecoveryInterval = TimeSpan.FromMinutes(10); pub.Options.SendBuffer = MegaByte * 10; // 10 megabyte pub.Connect("pgm://224.0.0.1:5555"); sub1.Options.ReceiveBuffer = MegaByte * 10; sub1.Bind("pgm://224.0.0.1:5555"); sub1.Subscribe(""); sub2.Bind("pgm://224.0.0.1:5555"); sub2.Options.ReceiveBuffer = MegaByte * 10; sub2.Subscribe(""); Console.WriteLine("Server sending 'Hi'"); pub.Send("Hi"); bool more; Console.WriteLine("sub1 received = '{0}'", sub1.ReceiveString(out more)); Console.WriteLine("sub2 received = '{0}'", sub2.ReceiveString(out more)); } Console.ReadLine(); }
public void sendMessage(Command cmd) { try { System.IO.MemoryStream ms = new System.IO.MemoryStream(); Serializer.Serialize <Command>(ms, cmd); Msg msg = new Msg(); msg.InitGC(ms.ToArray(), (int)ms.Length); publisher.Send(ref msg, false); } catch { } }
// private void FixedUpdate() // { // Debug.Log(_joint.currentForce); // } // Update is called once per frame private void Update() { _timeSinceLastPub += Time.deltaTime; if (_timeSinceLastPub >= 1 / ZmqPubRate) { var pubMsg = new Msg(); //var position = _hip.transform.position; //var position = _hip.position; //var velocity = _hip.velocity; var force = _joint.currentForce; //var rotation = _hip.rotation; //var angularvelocity = _hip.angularVelocity; var msgData = new MsgDatab { //Position = new[] { position.x, position.y, position.z }, //Velocity = new[] { velocity.x, velocity.y, velocity.z }, Force = new[] { force.x, force.y, force.z }, //Rotation = new[] { rotation.eulerAngles.x, rotation.eulerAngles.y, rotation.eulerAngles.z }, //Angularvelocity = new[] { angularvelocity.x, angularvelocity.y, angularvelocity.z }, ID = id }; var pubMsgPayload = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msgData)); pubMsg.InitGC(pubMsgPayload, pubMsgPayload.Length); _pub.Send(ref pubMsg, false); _timeSinceLastPub = 0; } string recvStr; if (_sub.TryReceiveFrameString(out recvStr)) { var recvJson = JsonConvert.DeserializeObject <Dictionary <string, string> >(recvStr); if (int.Parse(recvJson["ID"]) != id) { return; } var forceX = float.Parse(recvJson["force_x"]); var forceY = float.Parse(recvJson["force_y"]); var forceZ = float.Parse(recvJson["force_z"]); _hip.AddRelativeForce(forceX, forceY, -forceZ); //negative z because conversion from right-hand to left-hand coordinates } }
public void TestPubSub() { // Create publisher and bind it to a localhost address and port PublisherSocket publisher = _context.CreatePublisherSocket(); publisher.Bind("tcp://127.0.0.1:9999"); // Allow time to bind Thread.Sleep(1000); // Create a thread to subscribe to the published message Thread subscriber = new Thread(new ThreadStart(subscribe)); subscriber.Start(); // Publish a test message publisher.Send("Test"); Thread.Sleep(1000); // Kill the thread subscriber.Abort(); }
void SendInternal(string message) { publisher.Send(message); }
/// <summary> /// Send the mux message to listners. /// </summary> /// <param name="message">The message to dispatch.</param> public void Dispatch(Taurus.FeedMux message) { pubSocket.Send(message.ToByteArray <Taurus.FeedMux>()); }
private void OnHeartbeatTimerElapsed(object sender, NetMQTimerEventArgs e) { publisherSocket.Send(StreamingProtocol.HeartbeatTopic); }