最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Javascript: two connections per client? - Stack Overflow

programmeradmin0浏览0评论

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 pletely 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?

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 pletely 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?

Share Improve this question asked Dec 12, 2011 at 9:01 user393964user393964 6
  • how about a timer which has a timeout of 20000 ms and after it finished the server async request, sleeps for another 20000 ms? – user497849 Commented Dec 12, 2011 at 9:14
  • If the client breaks the http connection your server surely registers this. You should be able to catch this event. – bennedich Commented Dec 12, 2011 at 9:20
  • @bennedich: don't think an event is generated when the connection is closed, I don't see how the server can know about it. – user393964 Commented Dec 12, 2011 at 9:27
  • @DorinDuminica: I cant have it sleep for 20000ms because the async connection should be available all the time.. – user393964 Commented Dec 12, 2011 at 9:27
  • @Sled: Ok. setTimeout or setInterval should be able to just send an async request (XMLHttpRequest) every now and then without disrupting your other code. – bennedich Commented Dec 12, 2011 at 9:51
 |  Show 1 more ment

3 Answers 3

Reset to default 6

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 et, 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:

var heartbeatXhr = new XMLHttpRequest();
heartbeatXhr.open('GET', '/polling-url', true); // true for asynchronous

Use setInterval(yourHearbeatFunction, 20000), it doesn't lock Javascript execution thread. So, longpolled request will be processed as soon as it es.

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 pleted 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./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).

发布评论

评论列表(0)

  1. 暂无评论