发送http请求并接收推送数据 [英] Send an http request and receive push data

查看:127
本文介绍了发送http请求并接收推送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 Node.js 代码向另一台服务器发送一个 http 请求.使用下面的代码可以正常工作.我的问题是有问题的服务器应该推送数据,并且在请求后立即关闭连接.

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.

例如,如果我使用浏览器打开页面,我会从服务器获取推送数据,并且我的页面会被浏览器刷新.问题是:如何在我的 Node 服务器中获取跟随我的 http 请求的推送请求(来自其他服务器)?我尝试使用keep-alive",但这不会改变任何东西.

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.

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();

推荐答案

您的问题与更新实际上如何在技术上在浏览器中完成有关,有些含糊不清.除非您告诉我们实际发生了什么操作,否则我们无法告诉您如何复制浏览器操作.

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 是一种请求/响应协议,这意味着如果浏览器没有首先打开一个到服务器的链接(带有请求),服务器就无法推送"到浏览器.

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) 页面加载 javascript,它会定期对计时器发出新的 AJAX 请求.如果这是正在发生的事情,那么您很幸运,您所要做的就是捕获该 AJAX 请求的结构,然后在您的应用中,在同一个计时器上发出一个新的 HTTP 请求.

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) 该页面向服务器打开一个 websocket,该套接字接受来自服务器的传入数据.这有点不那么直接,但有很多库(想到 socket.io)来建立这种连接.

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.

这篇关于发送http请求并接收推送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆