Send an http request and receive push data

若如初见. 提交于 2019-12-12 16:22:00

问题


I want my Node.js code to send an http request to another server. It works fine using the code below. My problem is that the server in question then should push data, and the connection is closed right after the request.

For instance, if I open the page using my browser, I get push data from the server and my page is refreshed by the browser. The question is: How can I get the push requests (from the other server) that follow my http request in my Node server? I tried to keep the connection alive using "keep-alive", but this does not change anything.

Example code:

var http = require("http");
var agent = new http.Agent;

var options = {
  hostname: 'server.com',
  port: 80,
  path: '/',
  method: 'GET',
  agent: agent,
  headers: {
      'Connection':'keep-alive'
  }
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

回答1:


You've got some ambiguity in your question related to how the updates actually, technically are accomplished in the browser. We can't tell you how to replicate the browser action unless you tell us what action is actually occurring.

HTTP is a request/response protocol, which means that the server cannot "push" to the browser without the browser first opening a link to the server (with a request).

What it sounds like to me is that you want to create a custom client for a web page you don't own, and so you're having to build your application to behave like a web page because you don't have control over the server that is serving the page. Is that correct?

(As an aside, many websites have Terms of Service that prohibit the type of use you're trying to develop. Please verify that you are within bounds with your application)

If that's the case, then what you have to do is figure out how the browser is connecting to the server once the page is loaded. There are several ways this could occur:

1) The page loads javascript that periodically makes a new AJAX request on a timer. If this is what's going on, then you're in luck, all you have to do is capture the structure of that AJAX request and then, in your app, make a new HTTP request on the same timer.

2) The page opens up a websocket to the server, and that socket accepts incoming data from the server. This is a little less straight forward, but there are plenty of libraries (socket.io comes to mind) to make that sort of connection.

As a separate concern, for either of those two methods, the site may pass credentials for the browser to pass back and successfully make those connections, if so you'll have to be able to grab those credentials and use them.




回答2:


Data from server is at chunk

res.on('data', function (chunk) {

HTTP was thought to send data and close connection. If you are running server.com you can rewrite your app to keep the connection open (like a normal socket). But as others suggested you I think you could use other technologies such as web-sockets or long polling.



来源:https://stackoverflow.com/questions/21493181/send-an-http-request-and-receive-push-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!