JQuery & PHP - can I push from the server?

后端 未结 5 1297
野趣味
野趣味 2021-02-20 11:18

I am just starting to look at JQuery; until now everything has been PHP.

Just curious: if the server detects an event and wants to update the user\'s browser, can I do s

5条回答
  •  逝去的感伤
    2021-02-20 12:09

    You can use "comet" to do this. PHP is a terrible language to do Comet in, though. One of the more popular techniques to do Comet in PHP (that sort of works) is long polling.

    The idea with long polling is to create an AJAX request to the server. The server accepts the connection but doesn't respond (i.e.: a while loop with a sleep(1) in it) until an event takes place. This could be seconds, minutes, etc.

    In order to make long polling "work", though, you're going to have to make sure the connection doesn't time out very quickly, so set your execution time high (minutes, or unlimited if possible). You're also going to need to write code on the client that handles the server's disconnection/timeout. When that happens, a new request should be started.

    Hope this helps!

提交回复
热议问题