diff --git a/NatsClient.cs b/NatsClient.cs index 8e36c86..c8fdd89 100644 --- a/NatsClient.cs +++ b/NatsClient.cs @@ -168,15 +168,27 @@ public void Unsubscribe(int sid) } } - public bool Publish(string topic, string message) + public bool Publish(string topic, string message, string reply = "") { ThrowIfDisposed(); lock (stateLock) { - return WhenConnected((conn) => SendPublish(conn, topic, message)); + return WhenConnected((conn) => SendPublish(conn, topic, message, reply)); } } + public int Request(string topic, string message, Action handler) + { + ThrowIfDisposed(); + string inbox = Guid.NewGuid().ToString("N"); + var sid = this.Subscribe(inbox, (msg, reply) => + { + handler(msg, reply); + }); + this.Publish(topic, message, inbox); + return sid; + } + private void BuildConnectMsg(Uri natsUrl) { string user = null, pass = null; @@ -353,9 +365,9 @@ private void SendUnsubscribe(Connection conn, long sid) conn.Send("UNSUB " + sid + " 0"); } - private void SendPublish(Connection conn, string topic, string message) + private void SendPublish(Connection conn, string topic, string message, string reply) { - conn.Send(new string[]{ "PUB " + topic + " " + message.Length, message }); + conn.Send(new string[] { "PUB " + topic + " " + reply + " " + message.Length, message }); } private void Processing()