Created
March 10, 2015 22:03
-
-
Save ruslander/2268678d9693096b4826 to your computer and use it in GitHub Desktop.
Dynamic discovery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
private static readonly NetMQContext Context = NetMQContext.Create(); | |
static void Main(string[] args) | |
{ | |
Task.Factory.StartNew(() => | |
{ | |
using (var responder = Context.CreateResponseSocket()) | |
{ | |
var randomPort = responder.BindRandomPort("tcp://*"); | |
var advertizer = new NetMQBeacon(Context); | |
advertizer.Configure(9999); | |
advertizer.Publish(randomPort.ToString(), TimeSpan.FromSeconds(2)); | |
while (true) | |
{ | |
var message = responder.ReceiveString(); | |
Console.WriteLine("got by subscription " + message); | |
responder.Send("ok"); | |
} | |
} | |
}); | |
Task.Factory.StartNew(() => | |
{ | |
using (var receptor = new NetMQBeacon(Context)) | |
{ | |
receptor.Configure(9999); | |
receptor.Subscribe(""); | |
while (true) | |
{ | |
string peerName; | |
string port = receptor.ReceiveString(out peerName); | |
Console.WriteLine("heard from " + peerName + " " + port); | |
using (NetMQSocket client = Context.CreateRequestSocket()) | |
{ | |
var node = peerName.Replace(":9999", ""); | |
client.Connect(string.Format("tcp://{0}:{1}", node, port)); | |
client.Send("wtf"); | |
Console.WriteLine(client.ReceiveString()); | |
} | |
} | |
} | |
}); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment