开发者

comet style application with loops

开发者 https://www.devze.com 2023-01-31 23:51 出处:网络
Do all comet style applications开发者_StackOverflow中文版 require a loop somewhere in the application on the serverside to detect updates/changes?If no, please could you explain how the logic behind a

Do all comet style applications开发者_StackOverflow中文版 require a loop somewhere in the application on the serverside to detect updates/changes? If no, please could you explain how the logic behind a loopless comet style application would work?


This kind of application will always require a loop, you need to periodically check for new data etc. Of course you can make the "loop" non-blocking by using an even-loop based approach, but in the end there's still a loop somewhere.

Just think about it for a moment, how would you make it work without a loop? I sure can't imagine a way that doesn't utilize a loop somewhere.


Short answer is, no, not all require a loop on the serverside.

Instead you can use long-polling AJAX calls from the browser to request data,

at which the server simply responds with the data and the browser waits until the response is gotten before sending a new request.


The solution could be stream_set_blocking. Use any possible blocking resource to be suspended by OS and wait for appropriate interruption.

Client side:

  1. Ajax call to endpoint script (timeout for ajax e.g. 30 seconds - after 30 seconds initiate another one - after 30 seconds you will get response from server - script execution time reached)
  2. If you will get response during 30 seconds handle response (async) and open new connection (as in comet done - I saw it in cometD client)

Server setup:

  1. Setup apache timeouts (between request and data sent to 30-31 second), this is so apache will allow you to wait so much
  2. set apache to allow lot of child instances (concurrent users * 1.5), but you need to be sure that you have enough memory for this amount of apache instances (+ memory used by php children)

Script one:

  1. execution_time = 28
  2. set shutdown_function in order to send response (timeout, but formatted and understandable for ajax if You need it)
  3. you need to open file, empty one
  4. enable blocking mode using stream_set_blocking for file stream
  5. try read from file and you will get suspended until other process will write to file or timeout be reached.
  6. As soon as script gets content in file written from other process it will get back and will send response. (this will trigger another ajax call and another slept process)

Worst thing is that you need to think how to get multiple reader scripts reading from same bus (file) without disturbing each other. Also there could be that timeout will be exactly at that time when message will be written into bus.

(hope that this solution is not as bad as my English)

0

精彩评论

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