using NetMQ; using NetMQ.Sockets; using System; using System.Threading; namespace 消息传递库_NetMQ服务端 { internal class Program { public static void Main() { using (var publisher = new PublisherSocket()) { // 绑定到一个端口,等待订阅者连接 publisher.Bind("tcp://*:5555"); int messageCount = 0; while (true) { string message = $"Message {messageCount}"; Console.WriteLine($"Publishing: {message}"); publisher.SendFrame(message); Thread.Sleep(1000); // 每秒发送一次 messageCount++; } } } } }
using NetMQ; using NetMQ.Sockets; using System; namespace 消息传递库_NetMQ客户端 { internal class Program { static void Main(string[] args) { using (var subscriber = new SubscriberSocket()) { // 连接到发布者的地址 subscriber.Connect("tcp://localhost:5555"); // 订阅所有消息 subscriber.Subscribe(string.Empty); while (true) { string message = subscriber.ReceiveFrameString(); Console.WriteLine($"Received: {message}"); } } } } }