简体   繁体   English

带套接字的 Node.js REST

[英]Node.js REST with Sockets

The problem I am trying to solve is我试图解决的问题是

  • client makes a restful POST to node server.客户端对节点服务器进行安静的 POST。
  • node server communicates with another external server via socket.节点服务器通过套接字与另一个外部服务器通信。
  • when the socket response comes back from the other server - node server responds to client with the data received.当套接字响应从另一台服务器返回时 - 节点服务器用收到的数据响应客户端。

I can communicate to the client via REST and separately I can communicate to the external server via socket (response time is ~100ms).我可以通过 REST 与客户端通信,另外我可以通过套接字与外部服务器通信(响应时间约为 100 毫秒)。 But combining these results yields nothing.但是将这些结果结合起来却一无所获。

const sjsc = require('sockjs-client');
app.post('/form', function(req, res) {
    const srvc = sjsc('http://external.server:port/path');
    srvc.onopen = function () {
        srvc.send(testData);
    }

    srvc.onmessage = function(data) {
        console.log('received ', data);
        res.send(data);
    };
});

const srvc = sjsc('http://external.server:port/path');

this needed to be a let .这需要是一个let this is the only thing i changed and works perfectly.这是我唯一改变并且完美运行的东西。

let srvc = sjsc('http://external.server:port/path');

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

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