简体   繁体   English

如何保持HTTP连接活着?

[英]How to keep a HTTP connection alive?

有没有办法使用JavaScript保持HTTP连接?

In short, I think the concept of long lived http connections in javascript really revolve around a style of communication called COMET. 简而言之,我认为javascript中长期存在的http连接的概念实际上围绕着一种称为COMET的通信方式。 This can be achieved in several different ways, but essentially involves the client (using XmlHttp powers) requesting data from the server immediately, and the server withholding the response until some event triggers it. 这可以通过几种不同的方式实现,但实质上涉及客户端(使用XmlHttp权限)立即从服务器请求数据,服务器保留响应直到某个事件触发它。 Upon receipt of this response, the client immediately makes another request (which will once again hang at the server end until something needs sending). 收到此响应后,客户端立即发出另一个请求(将再次挂起在服务器端,直到需要发送内容)。 This simulates server push, but is effectively nothing more than a delayed response used in a clever way. 这模拟了服务器推送,但实际上只不过是以巧妙的方式使用的延迟响应。 In the worst case, there can be fairly high latency (ie 2 messages need sending, so the cycle must be twice repeated, with all the costs involved) but generally, if the messaging rate is low, this gives a reasonable impression of real-time push. 在最坏的情况下,可能存在相当高的延迟(即2个消息需要发送,因此循环必须重复两次,所有成本都涉及)但通常,如果消息传递速率较低,则会给出真实的合理印象。时间推动。

Implementing the server-side for this kind of communication is far from trivial, and requires a good deal of asynchronous communications, concurrency issues and the like. 为这种通信实现服务器端是非常简单的,并且需要大量的异步通信,并发性问题等。 It's quite easy to write an implementation that can support a few hundred users each on their own thread, but to scale to the thousands requires a much more considered approach. 编写一个可以在自己的线程上支持几百个用户的实现非常容易,但要扩展到数千个需要更加考虑的方法。

I note that the last answer was given in 2009. Oh, how I remember the days. 我注意到最后一个答案是在2009年给出的。哦,我多么记得那些日子。 But lots of good things have happened since then; 但从那以后发生了很多好事; so I'll add this just to let people know what to look for. 所以我会添加这个只是为了让人们知道要寻找什么。 HTTP 1.0 provided a "keep-alive" request property that meant that the connection should be maintained for further requests. HTTP 1.0提供了“保持活动”请求属性,这意味着应该为进一步的请求维护连接。 In HTTP 1.1, this became the default. 在HTTP 1.1中,这成为默认值。 You actually have to opt-out of it if you don't want to reuse the connection (and if you want to be nice about it). 如果你不想重用连接(如果你想对它好一点),你实际上必须选择退出它。

The new standard for "WebSockets" actually gives you a full-duplex persistent connection. “WebSockets”的新标准实际上为您提供了全双工持久连接。 WebSockets are supported in all up-to-date versions of popular browsers and you can even use them in MSIE if you install the Google Chrome Framework (which means Google software is actually doing the work). 所有最新版本的流行浏览器都支持WebSockets,如果您安装Google Chrome Framework(这意味着Google软件实际上正在开展工作),您甚至可以在MSIE中使用它们。 Microsoft says IE supports it in version 10, but I haven't tried it myself. 微软称IE在版本10中支持它,但我自己没有尝试过。 What you need then is something to connect to, like http://highlevellogic.blogspot.se/2011/09/websocket-server-demonstration_26.html 您需要的是连接到的东西,例如http://highlevellogic.blogspot.se/2011/09/websocket-server-demonstration_26.html

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

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