简体   繁体   English

在事件侦听器回调中未调用Restify res.send()

[英]Restify res.send () not called inside event listener callback

I am using restify 2.8.4, nodejs 0.10.36 and IBM MQ Light messaging. 我正在使用restify 2.8.4,nodejs 0.10.36和IBM MQ Light消息传递。

It is a RPC pattern, whenever the receiver has the result ready, it will emit an event and below restify POST route will capture the event and data, and reply with res.send () . 这是一种RPC模式,每当接收方准备好结果时,它将发出一个事件,而在以下条件下,重新调整POST路由将捕获该事件和数据,并使用res.send ()回复。

Issue is, res.send () is not trigger. 问题是,不会触发res.send () And I don't understand why res.send () isn't being triggered. 而且我不明白为什么未触发res.send () There is only one res.send () in this POST route. 此POST路由中只有一个res.send ()

Tried res.write () and res.end () . 尝试过res.write ()res.end () Same thing. 一样。 Not triggered. 未触发。

Any advice is much appreciated. 任何建议深表感谢。

route.js route.js

server.post ('/user', function (req, res, next) {
  var body = req.body;

  var recvClient = createRecv ();

  var sendClient = createSend (body);

  recvClient.on ("receiver:got:it", function () {
    // receiver has confirmed received message
    // so sender client can be released 
    console.log ('stopping send client...'.info);
    sendClient.stop ();
  });

  recvClient.on ("receiver:replied", function (data) {
    // event callback was triggered
    console.log ('receiver replied captured...');

    res.send ();
    return next ();
  });

});

receiver.js receiver.js

recvClient.on('started', function() {
    recvClient.subscribe(topicPattern, {qos: 1, autoConfirm: false, credit: 1});

    recvClient.on('message', function(data, delivery) {
      console.log('Recv: ' + util.inspect (data, false, null));

      delivery.message.confirmDelivery (function () {
        recvClient.emit ('receiver:got:it'); 
      });

      var user = new User (data);     
      user.create ()           
      .then (function (user) { 
        recvClient.emit ('receiver:replied', user);
    }, function (err) {      
        recvClient.emit ('receiver:replied', {err: err.message});
    })
      .finally (function (){   
        console.log ('stopping receiver client...');
        recvClient.stop ();    
      });

    });
  });

Output trace 输出轨迹

body: {}  //stuff here
Recv: {}  // receiver received body stuff
stopping send client...
// do work

receiver replied captured...
stopping receiver client...

Update 1 更新1

If I replace event emitter with another send/receive message queue from the receiver, res.send () is called. 如果我将事件发射器替换为接收器中的另一个发送/接收消息队列,则会调用res.send ()

This approach is messier than event emitter. 这种方法比事件发射器更混乱。 I still refuse to believe why event emitter won't work. 我仍然拒绝相信事件发射器为何不起作用。

var rpcReceive = new RpcReceive ("user.create.replied", {qos:1, autoConfirm: false, credit: 1});

rpcReceive.receive (recvClient)
.then (function (user){
  res.send (user);
  return next ();
})
.then (undefined, function (err){
  return next (new restify.errors.BadRequestError (err.message));
});

The problem looks like in the structure of route.js. 该问题看起来像是route.js的结构。 Actually, recvClient.on event should not be dependent on server.post('/user'). 实际上,recvClient.on事件不应依赖于server.post('/ user')。 Correct structure should be like: 正确的结构应为:

var recvClient = createRecv ();     // should be global object

server.post ('/user', function (req, res, next) {
  var body = req.body;

  //var recvClient = createRecv ();     //commented out this code as this is moved out of this API.

  //var sendClient = createSend (body); //commented out this code. May be this wont be required.

});

recvClient.on ("receiver:got:it", function () {
    // receiver has confirmed received message
    // so sender client can be released 
    console.log ('stopping send client...'.info);
    //sendClient.stop ();       //commented out this code. May be this wont be required.
});

recvClient.on ("receiver:replied", function (data) {
    // event callback was triggered
    console.log ('receiver replied captured...');

    //res.send ();              
    //return next ();       

    /*commented out these two lines. Instead of sending response like this, emit some event from here, which can be consumed on receiver.js. Use something like below code to send response to receiver.js */
    recvClient.emit('sending:response', "response");
});

And your receiver.js should have below logic (in addition to your code) to receive response from route.js. 而且您的receive.js应该具有以下逻辑(除了代码之外),以接收来自route.js的响应。

recvClient.on('sending:response', function(response) {
    // do processing on response
});

Please try this and response back in case you find issue. 请尝试此操作,如果发现问题,请回复。 I have worked on similar logic to emit and consume events while creating chat application. 我已经在创建聊天应用程序时使用类似的逻辑来发出和使用事件。 Please let me know in case you still face problem. 如果您仍然遇到问题,请告诉我。

您可以尝试使用res.json({'status':200})吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM