开发者

How to use long-polling (preferably without Comet or node.js)?

开发者 https://www.devze.com 2023-03-26 08:41 出处:网络
Suppose I\'m synchronizing text on an HTML page, stored in localStorage from computer A to computer B. When the user edits the text on computer A, I send a request to the server, and tell it what text

Suppose I'm synchronizing text on an HTML page, stored in localStorage from computer A to computer B. When the user edits the text on computer A, I send a request to the server, and tell it what text changed. Is there any way that I can get the server to tell computer B that there's new text, without computer B having to keep checking? I've read about Comet, but I'm trying no开发者_JS百科t to use any libraries. Is there a way? Also, if you're going to suggest Comet, could you give a simple example using plain JavaScript and Comet?

I had an idea, but I don't know if it'd work. On computer B, I send a request once, and let the server's file keep delaying the response till there is some new text. That way, it'd be like long-polling, but the request would eventually timeout. If there was a way to set the timeout to be never, this would get much easier. So, anyone have any ideas?

P.S.- I want to use only JavaScript and PHP and try avoid using libraries. Still, if you know of a library that can do this with JS and PHP, please tell me anyway.

Also, I know there have been questions like this, but the answers I found there weren't satisfactory.

Oh, and if anyone knows how the chat over here works, could you tell me that as well?

tl;dr: I want to send a request from a server to an HTML page, or have the HTML send a single request which is kept alive till an appropriate response is created.


Comet is an implementation of long polling.

The issue is the host machine needs to keep all the resources available for all of the open requests. Servers must be carefully configured to allow this to work smoothly under load. This is not an "out of the box" solution.

Another drawback is each user must be bound to a particular server - so there's no easy way to load-balance requests.

Sticking with periodic polling is often the best bet.

0

精彩评论

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