我正在开发一个node.js应用程序,它试图从socket.io客户端获取数据,以便从另一个客户端填充对POST请求的响应。有点像...
客户端A,发出初始POST请求,是一个Web服务(Plivo / plivo-node),所以我无法改变它如何命中服务器。
在POST请求中调用的节点代码如下所示......
app.handlePlivoRequest = function (req, res) {
// create Plivo response object
var r = plivo.Response();
// set listener for client response
client.socket.on('callResponse', function(msg){
// add msg data from client to the Plivo response
r.addSpeak(msg);
});
// forward request to socket client
client.socket.emit('call', req.body );
// render response as XML for Plivo
return r.toXML();
}
我遇到的问题是handlePlivoRequest返回而不等待客户端的响应。
任何人都可以帮我解决这个问题,以便等待套接字响应吗?
谢谢!
答案 0 :(得分:0)
您需要在套接字响应回调中结束http请求:
app.handlePlivoRequest = function (req, res) {
// create Plivo response object
var r = plivo.Response();
// set listener for client response
client.socket.on('callResponse', function(msg){
// add msg data from client to the Plivo response
r.addSpeak(msg);
// render response as XML for Plivo
res.set({ 'Content-Type' : 'text/xml'});
res.end(r.toXML());
});
// forward request to socket client and wait for a response..
client.socket.emit('call', req.body );
}