我创建了 2 个简单的 C# 控制台项目(.net 4.5.2),向每个项目添加了 v4.0.0.1 NetMQ Nuget 包,将每个程序加载到单独的 Visual Studio 2017 社区版中,在包含在OnReceiveReady 回调方法,先启动订阅者程序,再启动发布者程序。订阅者中未触发 ReceieveReady 事件。我究竟做错了什么?即使我选择了 subSocket.Subscribe("") ,我仍然没有收到任何消息。此外,删除/修改 Send/Receive HighWatermarks 也没有改变。谢谢你的帮助!
这是发布者代码:
using System;
using NetMQ;
using NetMQ.Sockets;
using System.Threading;
namespace SampleNQPub
{
class Program
{
static void Main(string[] args)
{
var addr = "tcp://127.0.0.1:3004";
using (var pubSocket = new PublisherSocket())
{
Console.WriteLine("Publisher socket binding.");
pubSocket.Options.SendHighWatermark = 10;
pubSocket.Bind(addr);
for (int i=0; i < 30; i++)
{
pubSocket.SendMoreFrame("NQ").SendFrame(i.ToString());
Thread.Sleep(1000);
}
pubSocket.Disconnect(addr);
}
}
}
}
这是订阅者代码:
using System.Threading;
using NetMQ;
using NetMQ.Sockets;
namespace SampleNQSub
{
class Program
{
static void Main(string[] args)
{
var addr = "tcp://127.0.0.1:3004";
using (var subSocket = new SubscriberSocket())
{
subSocket.ReceiveReady += OnReceiveReady;
subSocket.Options.ReceiveHighWatermark = 10;
subSocket.Connect(addr);
subSocket.Subscribe("NQ");
for (int i=0; i < 20; i++)
{
Thread.Sleep(1000);
}
subSocket.Disconnect(addr);
}
}
static void OnReceiveReady(object sender, NetMQSocketEventArgs e)
{
var str = e.Socket.ReceiveFrameString();
}
}
}