简体   繁体   中英

Node.js REST with Sockets

The problem I am trying to solve is

  • client makes a restful POST to node server.
  • 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). 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 . this is the only thing i changed and works perfectly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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