从 zmq 套接字返回值 [英] return value from zmq socket

查看:23
本文介绍了从 zmq 套接字返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Socket.IO 让客户端网页从我的 Node.JS 服务器动态获取数据.在服务器端,我使用 zeromq 套接字库(REQ/REP 结构,请参阅 zguide 中的 rrclient.js 示例)将请求转发到 Python 后端.

I am using Socket.IO to have a client webpage fetch data dynamically from my Node.JS server. On the server-side, I am forwarding the request to a Python backend using the zeromq socket library (REQ/REP structure, see the rrclient.js example from zguide).

但是,由于我的服务器充当 socket.io 服务器,因此我必须按如下方式包装响应才能将其发送回特定客户端:

However, since my server is acting as the socket.io server, I have to wrap my response as follows in order to send it back to the specific client:

socket.on('app_request',function(appRequest){
      //route this request to the backend.
      var results = fetch_data(appRequest);
      //return it to the client
      socket.emit('app_results',results);
  });

因此 fetch_data 函数使用 zmq 套接字(socket.send)将请求推送到服务器.

so the fetch_data function uses a zmq socket (socket.send) to push a request to the server.

但是节点 ZMQ 套接字使用 socket.on('message',function(){}); 响应请求.listener 并且实际上不返回任何内容.

But the Node ZMQ socket responds to request using a socket.on('message',function(){}); listener and doesn't actually return anything.

如何使用 socket.on('message',function(){});向发起 socket.send() 请求的特定函数返回一个值?

换句话说,我怎样才能获得客户端的 socket.io 请求,让 Node.JS 使用 ZMQ 从 python 后端获取相关数据,然后将其返回给 socket.io 客户端?(当然以非阻塞方式)

In other words, how can I get a client's socket.io request to have Node.JS fetch relevant data from a python backend using ZMQ, then return it back to the socket.io client? (in a non-blocking fashion of course)

推荐答案

我做过类似的事情,你会想像这样重做;

I've done something similar, you'll want to rework it like so;

socket.on('app_request',function(appRequest){
  //route this request to the backend.
  fetch_data(appRequest, function(err, results) {
       socket.emit('app_results',results);
  });
});

zmq 是非阻塞的,因此在您将其发送到 socket.io 之前不会收到响应,使用此方法您可以调整 fetch_data 方法来存储回调,直到返回响应然后触发它.

zmq is non-blocking so the response is not received before your sending it down to socket.io, using this method you can adjust your fetch_data method to store the callback until the response is returned and then fire it.

粗略的 fetch_data 仅供参考,未测试;

Rough fetch_data for inspiration only not tested;

var zmq = require('zmq'),
    req = zmq.socket('req'),
    count = 0,
    jobs = {};


req.on('message', function(response) {
    var args  = JSON.parse(response),
        reply = args[0],
        id    = args[i];

    if (id && jobs[id]) {
         jobs[id].apply(null, Array.isArray(reply) ? reply : [reply]);

         delete jobs[id];
    }
}

function fetch_data(appRequest, callback) {
    var data = JSON.stringify([appRequest, count]);

    jobs[count] = callback;

    req.send(data);

    ++count;
}

然后在另一端解析 json 并保留 id 并将其与响应一起发回.

Then on the other side parse the json and keep the id and send it back with the response.

这篇关于从 zmq 套接字返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆