使用 Node.js 跟踪 XMLHTTPRequest
我刚刚开始学习 Node.js,对其实时功能非常感兴趣,尤其是 Socket.io。从那时起,我编写了一个非常基本的脚本来连接到 Twitter 的流服务器并向所有连接的客户端广播推文。
为了构建它,我使用http.createClient
连接到stream.twitter.com并添加了相关的response
和data
事件处理程序。一切都运转良好。
显然,Twitter 的 Streaming API 几乎会输出一个无限加载的网页,以及为什么使用 data
事件处理程序可以很好地配合它。但是,是否有可能使其他类型的网站“可流式传输”?
例如,如果客户端(浏览器)使用 XMLHTTPRequest 定期更新网站,是否可以使用 Node.js 的 HTTP API 跟踪这些请求的输出?或者类似的 Node.js 扩展?
谢谢。
I've just started learning Node.js and was very interested in its real-time capabilities, especially with Socket.io. Since then, I've written a very basic script to connect to Twitter's streaming server and broadcast tweets to all connected clients.
To build that, I used http.createClient
to connect to stream.twitter.com and added in the relevant response
and data
event handlers. Everything works quite well.
Obviously, Twitter's Streaming API pretty much outputs an infinitely loading webpage and what why using a data
event handler works fairly well with it. However, is it possible to make other types of websites 'streamable'?
For example, if a client (browser) updates a website periodically using an XMLHTTPRequest, would it be possible to track the output of those requests using the HTTP API of Node.js? Or similar Node.js extension?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
网站不定期使用 XMLHTTPRequest。客户端定期向 URL 发送 XMLHTTPRequest。
使用正确的标头对
http.request(options,callback)
进行简单调用应该可以模拟 XHR。大多数这些服务器还将接受普通的 POST 或 GET 请求。如果您希望 Node.js 连接到服务器并模拟浏览器,那么像 zombie 这样的东西会很好地为您服务。它声称支持 XMLHTTPRequest。
websites do not periodically use XMLHTTPRequest. Clients periodically send XMLHTTPRequests to an URL.
A simple call to
http.request(options, callback)
with the correct headers should emulate XHR's. Most of these servers will also accept normal POST or GET requests.If you want node.js to connect to a server and simulate a browser then something like zombie would serve you well. It claims to support XMLHTTPRequest.
最适合您的情况是在仪表板和节点服务器之间使用 Web 套接字。这样,节点将立即收到通知,您的仪表板中的某些内容已更新(我假设您可以稍微修改您的仪表板以接受此类连接,只要您有权访问,就不会有困难)。
然后您可以在客户端使用长轮询,即向节点服务器发送请求并等待。节点将接收请求,然后向其注册一个事件。当它从仪表板收到更新时,它将触发事件,该事件将响应一一发送给所有等待的客户端。
我建议您查看 http://github.com/andrewdavey/vogue 。它做了类似的事情,但功能当然不同。
The best case for you would be to use web-sockets between your dashboard and node server. This way node will be notified immediately that something has updated at your dashboard ( I am assuming that you can modify your dashboard a bit to accept such connections, won't be difficult as long as you have access).
Then you can use long polling at client-end i.e. send a request to the node server and wait. Node will receive the request and then register an event to it. The moment it receives the updates from dashboard, it'll fire the event which will send the response to all the clients one by one waiting.
I would recommend take a look at http://github.com/andrewdavey/vogue . It does something similar but the functionality is ofcourse different.