开发者

How does Facebook chat avoid continuous polling of the server? [closed]

Want to improve this post? Add citations from reputable s开发者_StackOverflow社区ources by editing the post. Posts with unsourced content may be edited or deleted. As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.

I am trying to understand how Facebook's chat feature receives messages without continuously poling the server.

Firebug shows me a single GET XmlHttpRequest continuously sitting there, waiting for a response from the server. After 5 minutes, this never timed out.

How are they preventing timeout?

An AJAX request can just sit there like that indefinitely, waiting for a response?

Can I do this with JSONRequest? I see this at json.org:

JSONRequest is designed to support duplex connections. This permits applications in which the server can asynchronously initiate transmissions. This is done by using two simultaneous requests: one to send and the other to receive. By using the timeout parameter, a POST request can be left pending until the server determines that it has timely data to send.

Or is there another way to let an AJAX call just sit there, waiting, besides using JSONRequest?


Facebook uses a technique which is now called Comet to push messages from the server to the client instead of having the client poll the server.

There are many ways that this can be implemented, with XMLHttpRequest long polling being just one option. The principle behind this method is that the client sends an ordinary XMLHttpRequest but the server doesn't respond until some event happens (such as another user sending a message), so the client is forced to wait. When the client receives a response (or if the request times out) the client simply creates a new request so that it always has one open request to the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜