Javascript:每个客户端有两个连接?
我正在开发一个实现长轮询的应用程序,因为我希望用户在通知到达时立即收到通知。我已经完成了这部分工作,但我还需要使用 JavaScript 函数来扩展它,该函数每 20 秒向服务器发送一次“心跳”。
我的问题:如何在不完全中断代码 20 秒的情况下执行此操作(以便其他 javascript 在计数时仍然执行),是否有某种方法可以为此使用第二个连接?因为我不希望当心跳发送给用户时我的长轮询被中断。
有什么想法吗?
I'm working on an application which implements longpolling because I want the user to receive notifications as soon as they arrive. I have this part working, but I also need to extend this with a javascript function which sends a 'heartbeat' to the server every 20 seconds.
My question: how do I do this without completely interrupting the code for 20 seconds (so that other javascript is still executed while it's counting) and is there some way to use a second connection for this? Because I don't want my longpolling to be interrupted when the heartbeat is sent to the user.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然下面的线程相当旧,但它表明浏览器允许在任何时间通过 XMLHttpRequest 进行多个连接。
允许多少个并发 AJAX (XmlHttpRequest) 请求在流行的浏览器中?
如果您使用 XMLHttpRequest 或任何其他实现 comet 的方式进行长轮询会话,您仍然可以向同一服务器创建另一个请求作为setInterval 或 setTimeout 函数,直到达到浏览器施加的有限限制,您需要确保的是在 XHR 的构造中包含异步标志:
While the thread below is quite old, it shows that browsers allow multiple connections via XMLHttpRequest at any one time.
How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?
If you have a long polling session either using an XMLHttpRequest or any other means of implementing comet, you can still create another request to the same server as part of a setInterval or setTimeout function until you reach the finite limit imposed by the browser, all that you would need to ensure is that you include the asynchronous flag in the construction of the XHR:
使用
setInterval(yourHearbeatFunction, 20000)
,它不会锁定Javascript执行线程。因此,长轮询请求一到来就会被处理。Use
setInterval(yourHearbeatFunction, 20000)
, it doesn't lock Javascript execution thread. So, longpolled request will be processed as soon as it comes.您不需要为您想要实现的目标维护多个连接。 Javascript 的异步特性允许连接在处理其他事情时保持活动状态。您可能认为 javascript 的 XHR 是阻塞的,因为它是单线程的。
由于事件循环模型,JavaScript 中的 XHR 是非阻塞的 - JavaScript 引擎不断地处于循环中,检查注册的调用是否已完成以及是否应处理其回调。这允许其操作是非阻塞的,从而允许单个 JavaScript 应用程序处理多个 XHR 长轮询请求。
如果您能够使用 jQuery 进行请求,它可以使用此函数很好地包装 XHR: http:// /api.jquery.com/jQuery.ajax/。这样,您可以定义 20 秒的超时并立即处理(以重新启动与服务器的连接)。
旁白 - 您可能希望考虑您的服务器堆栈来优化长轮询。确保您的 Web 服务器不会为每个请求生成一个线程(如 Apache 2.2) - 否则您将很快耗尽系统资源!如果您可以使用node.js(这对于处理许多并发请求非常有用),请研究socket.io库作为服务器端和客户端解决方案(http://socket.io/#home)。
You don't need to maintain multiple connections for what you're trying to achieve. Javascript's asynchronous nature allows the connection to stay alive while other things are being processed. You may have thought that javascript's XHR's were blocking because it is single threaded.
The XHR's in javascript are non-blocking because of the event loop model - the javascript engines are constantly in a loop, checking to see whether registered calls have completed and whether its callbacks should be processed. This allows its operations to be non-blocking and thus allowing a single javascript application to process multiple XHR long polling requests.
If you are able to use jQuery for the request, it wraps the XHR quite nicely with this function: http://api.jquery.com/jQuery.ajax/. With this, you can define a timeout of 20 seconds and handle it immediately (to reinitiate the connection with your server).
Aside - You may wish to consider your server stack to optimize long polling. Make sure that your web server does not spawn a thread per request (like Apache 2.2) - otherwise you will quickly run out of system resources! If you can use node.js (which is great for handling many simultaneous requests), look into socket.io library as a server-side and client-side solution (http://socket.io/#home).