What is a safe amount of time that I can wait befo

2020-07-16 12:23发布

问题:

I'm making a chat application that works with long-polling to emulate a "push" from server to client.

Basically, the browser asks for updates, and I reply if there's something new. Otherwise, I keep the connection open without responding until there is something to send back.

Now, if 30 seconds have passed and I haven't sent anything, then I do send a response, saying basically "NoNews", and the client will poll again.

What I want to do is obviously keep this connection without replying for as long as possible, before the browser will simply time out and give up on me... I haven't found good documentation on what the Client Timeout is for each browser, and it doesn't seem like it's the same for all of them...

Has any of you made a long-polling application?
Any ideas what the longest safe timeout might be?

Thanks!

回答1:

The browser should detect a timeout on an XHR and make another request.

Update:

Detecting timeouts on an XHR is actually complicated, since it's not built-in for some reason. Of course you will also need to handle 502/503 responses, etc..

  • http://ajaxpatterns.org/XMLHttpRequest_Call
  • http://geekswithblogs.net/lorint/archive/2006/03/07/71625.aspx
  • http://ajaxblog.com/archives/2005/06/01/async-requests-over-an-unreliable-network


回答2:

The read timeout varies between browsers. For example, these are default values for IE,

Internet Explorer 4.0 and Internet Explorer 4.01 - 5 minutes
Internet Explorer 5.x and Internet Explorer 6.x - 60 minutes
Internet Explorer 7 and Internet Explorer 8 - 60 minutes

As you can see, it gets larger overtime.

In long polling, timeout is your friend. You should take advantage of it, instead of avoiding it. Timeout means you are doing LONGEST polling possible with the browser. Timeout is an error you have to handle even without long polling so there is no extra burden.

You might want read my response to this question,

polling a HTTP server from J2ME client

Even though it's for a mobile client, most rules apply to AJAX long polling also. Specifically, I think you will benefit from a notification system so long polling is only used for event notification and all contents are still pulled normally.