0

场景:我可以使用以下代码通过 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_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 中正确接收。

问题:谁能告诉我我做错了什么?请稍微说明一下这个问题。

高级感谢

4

1 回答 1

0

它通过使用不同的套接字类型 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 拉取代码:

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
于 2013-04-02T07:28:17.870 回答