无法从ZeroMQ (ØMQ)接收确认

本文关键字:MQ 确认 #216 ZeroMQ | 更新日期: 2023-09-27 18:06:53

场景:我能够通过ZeroMQ (ØMQ)使用以下代码将字符串类型的数据从c#发送到Node.JS应用程序:
c#推送代码:

using (var context = new Context(1))
                {
                    using (Socket client = context.Socket(SocketType.PUSH))
                    {
                        client.Connect("tcp://127.0.0.1:12345");
                        int i = 0;
                        while (true)
                        {
                            string request = i.ToString() + "_Hello_ ";
                            i++;
                            Console.WriteLine("Sending request..." + i.ToString());
                            client.Send(request, Encoding.Unicode); 
                              // <== Here is the issues    
                            string reply = client.Recv(Encoding.Unicode).ToString(); 
                              // <== Here is the issues
                            Console.WriteLine("Received reply :", reply);
                        }
                    }
                }

Node.JS pull code:

pull_socket.bindSync('tcp://127.0.0.1:12345')
pull_socket.on('message', function (data) {
    i++;
    console.log(i.toString() + 'received data:'n');
    console.log(data.toString());
    pull_socket.send('rcvd'); // <== Here is the issues
});

问题:在c#中reply对象将包含字符串"Not supported",但发送的数据将在Node.js中正确接收。

问题:谁能告诉我哪里做错了?请解释一下这个问题。

先进谢谢

无法从ZeroMQ (ØMQ)接收确认

通过使用不同的套接字类型REQ/REP而不是PUSH/PULL来解决这个问题
代码如下:
c#推送代码:

using (var context = new Context(1))
                {
                    using (Socket client = context.Socket(SocketType.REQ))
                    {
                        client.Connect("tcp://127.0.0.1:12345");
                        int i = 0;
                        while (true)
                        {
                            string request = i.ToString() + "_Hello_ ";
                            i++;
                            Console.WriteLine("Sending request..." + i.ToString());
                            client.Send(request, Encoding.Unicode); 
                            string reply = client.Recv(Encoding.Unicode).ToString();
                            Console.WriteLine("Received reply :" + reply);
                        }
                    }
                }

Node.JS pull code:

var rep_socket = zmq.socket('rep')
rep_socket.bindSync('tcp://127.0.0.1:12345')
rep_socket.on('message', function (data) {
    i++;
    console.log(i.toString() + 'received data:'n');
    console.log(data.toString());
    pull_socket.send('hello WORLD'); // <== Here is the issues
});

c#控制台输出:

Sending request...0
Received reply: hello WORLD