简体   繁体   English

多次发送回复?

[英]Send response multiple times?

I have a little problem. 我有一点问题。

I have a node application on a server, that serves a website as control panel for a client based application. 我在服务器上有一个节点应用程序,该服务器将网站用作基于客户端的应用程序的控制面板。

Example: 例:

  1. You click a link on the site 您单击网站上的链接
  2. A http request triggers the server to send a command via socket to the client http请求触发服务器通过套接字向客户端发送命令
  3. The client executes this command 客户端执行此命令
  4. The client sends the output of the command back to the server (socket) 客户端将命令的输出发送回服务器(套接字)

That works fine, on time. 按时工作正常。 The second time the server crashes because the header is already sent. 服务器第二次崩溃,因为标头已经发送。

//Comand comes in
app.get('/create/:id/:package', function (req, res) {
    if (io.sockets.connected[req.params.id]) {
        //Command is sent to client
        io.sockets.connected[req.params.id].emit('control', {type: 'create', packageName: req.params.package});
        //Output is coming back
        socket.on('createOutput', function (data) {
            //Response sent back to http request (works once)
            res.send(data);
        });
    }
});

Is there any way to work around this behavior? 有什么办法可以解决此问题?

You can send the data only once with socket.once : 您只能使用socket.once发送一次数据:

socket.once('createOutput', function (data) {
        //Response sent back to http request (works once)
        res.send(data);
    })

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

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