场景:我可以使用以下代码通过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中正确接收。
问题:可以告诉我我做错了吗?请详细说明这个问题。
高级谢谢
答案 0 :(得分: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