Node.js 将json对象传递给zmq服务器
您好,我基本上使用zmq模块编写了一个客户端和一个服务器。我能够使用我的客户端向服务器发送一个普通字符串作为输入,数据显示良好。但是当我传递一个JSON对象时,它的打印方式不同。下面是我的客户端代码 客户端代码 } }); 请求服务器中的数据Node.js 将json对象传递给zmq服务器,node.js,zeromq,Node.js,Zeromq,您好,我基本上使用zmq模块编写了一个客户端和一个服务器。我能够使用我的客户端向服务器发送一个普通字符串作为输入,数据显示良好。但是当我传递一个JSON对象时,它的打印方式不同。下面是我的客户端代码 客户端代码 } }); 请求服务器中的数据 收到的请求:[] 而我正在发送的数据是{“AdvisorId”:“71864”,“Phone”:“952-921-4972”},这是一个JSON对象。我被困在这里,关于这方面的任何帮助都会非常有用。ZeroMq支持字符串和字节[]数据,而不是对象 试试这
收到的请求:[]
而我正在发送的数据是{“AdvisorId”:“71864”,“Phone”:“952-921-4972”},这是一个JSON对象。我被困在这里,关于这方面的任何帮助都会非常有用。ZeroMq支持字符串和字节[]数据,而不是对象 试试这个:
requester.send("{AdvisorId" : "71864", "Phone": "952-921-4972}");
这会有用的
requester.connect("tcp://localhost:7000");
for (var i = 0; i < 5; i++) {
console.log("Sending request", i, '...');
requester.send({"AdvisorId" : "71864", "Phone": "952-921-4972"});
}
process.on('SIGINT', function() {
requester.close();
});
// Hello World server
// Binds REP socket to tcp://*:7000
// Expects "Hello" from client, replies with "world"
var zmq = require('zmq');
// socket to talk to clients
var responder = zmq.socket('rep');
responder.on('message', function(request) {
console.log("Received request: [", request, "]");
console.log(typeof request)
// do some 'work'
setTimeout(function() {
// send reply back to client.
responder.send("world");
}, 1000);
});
responder.bind('tcp://*:7000', function(err) {
if (err) {
console.log(err);
} else {
console.log("Listening on 7000...");
}
});
process.on('SIGINT', function() {
responder.close();
});
Received request: [ <SlowBuffer 5b 6f 62 6a 65 63 74 20 4f 62 6a 65 63 74 5d> ]
requester.send("{AdvisorId" : "71864", "Phone": "952-921-4972}");
var jsonObject={"AdvisorId" : "71864", "Phone": "952-921-4972"} ;
var stringObject=JSON.stringify(jsonObject);
requester.send(stringObject);