簡體   English   中英

在事件偵聽器回調中未調用Restify res.send()

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

我正在使用restify 2.8.4,nodejs 0.10.36和IBM MQ Light消息傳遞。

這是一種RPC模式,每當接收方准備好結果時,它將發出一個事件,而在以下條件下,重新調整POST路由將捕獲該事件和數據,並使用res.send ()回復。

問題是,不會觸發res.send () 而且我不明白為什么未觸發res.send () 此POST路由中只有一個res.send ()

嘗試過res.write ()res.end () 一樣。 未觸發。

任何建議深表感謝。

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

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 ();    
      });

    });
  });

輸出軌跡

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

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

更新1

如果我將事件發射器替換為接收器中的另一個發送/接收消息隊列,則會調用res.send ()

這種方法比事件發射器更混亂。 我仍然拒絕相信事件發射器為何不起作用。

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));
});

該問題看起來像是route.js的結構。 實際上,recvClient.on事件不應依賴於server.post('/ user')。 正確的結構應為:

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");
});

而且您的receive.js應該具有以下邏輯(除了代碼之外),以接收來自route.js的響應。

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

請嘗試此操作,如果發現問題,請回復。 我已經在創建聊天應用程序時使用類似的邏輯來發出和使用事件。 如果您仍然遇到問題,請告訴我。

您可以嘗試使用res.json({'status':200})嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM