2017-02-02 8 views
0

私はtsoaを使用していため闊歩定義を生成し、私は闊歩のAPIのRabbitMQ応答キューオブジェクト

import { configuration, random, uuid, amqp } from '../config'; 
const EventEmitter = require('events'); 

const REPLY_QUEUE = 'amq.rabbitmq.reply-to'; 
const createClient =() => amqp.connect(configuration.amqpUrl) 
    .then((conn) => conn.createChannel()) 
    .then((channel: any) => { 
     // create an event emitter where rpc responses will be published by correlationId 
     channel.responseEmitter = new EventEmitter(); 
     channel.responseEmitter.setMaxListeners(0); 
     channel.consume(REPLY_QUEUE, 
      (msg: any) => channel.responseEmitter.emit(msg.properties.correlationId, msg.content), 
      { noAck: true }); 
     return channel; 
    }); 

const sendRPCMessage = (channel: any, message: any, rpcQueue: any) => new Promise((resolve, reject) => { 
    const correlationId = uuid.v4(); 
    // listen for the content emitted on the correlationId event 
    try { 
     channel.responseEmitter.once(correlationId, resolve); 
     channel.sendToQueue(rpcQueue, new Buffer(JSON.stringify(message)), { correlationId, replyTo: REPLY_QUEUE }) 
    } catch (error) { reject(error) } 

}); 

のRabbitMQを接続するためのfacundoolano実装を使用していますが、問題は、私はわかりませんでしたのRabbitMQへのAPIリクエストを送信しようとしています要求の応答を返す方法。私のコントローラは、私がどのように正しい方法のRabbitMQの応答キュー

答えて

1
@Post() 
public async postSurvey(survey: Survey): Promise<Survey> { 
    return createClient() 
    .then(channel => { 
     return sendRPCMessage(channel, survey, "survey_q_req") 
    }) 
    .then(data => { 
     return data; 
    }) 
    .catch((error) => { console.log(error) }); 
} 
からの応答を返すために、この

@Post() 
    public async postSurvey(survey: Survey): Promise<Survey> { 
     var obj: Survey; 
     await createClient().then((channel) => { 
      sendRPCMessage(channel, survey, "survey_q_req").then((data) => { 
       obj = data as Survey; 
      }).catch((error) => { console.log(error) }); 
     }).catch((error) => { console.log(error) }); 
     return obj 
    } 

のようなキュー応答を返すようにしようとしました