问题
How may I refresh current client view page from the server side? for example, if I want to force refresh for page that is viewing now for all the clients, so they can view the new content. and I want to do this refresh in the time that I want using PHP.
Is it possible??
回答1:
Use Web Sockets. You can use either PHP Sockets with this or this
I think php Sockets would require you to have a PHP version of 5.3 or above.
If you don't have PHP v5.3 or above you can use Node JS with Socket.io
Long Pooling is also one of the option, but that won't be a good solution considering the number of requests your server would be getting. Also the user's bandwidth usage will go high.
回答2:
You can get the behavior in two ways:
WebSockets - If all your clients connect via web sockets, you have a direct connection between the client and the server. You'll need to get creative with timing, but you can certainly send a command at any time requiring a refresh. This requires that you implement web sockets in PHP http://socketo.me/ and JavaScript.
Polling - You can set up the client to send an AJAX request every x seconds. The server responds with a bool value which the AJAX success handler uses to determine whether it should refresh the page.
回答3:
If you want to refresh the page at a certain interval, include this in your <head>
:
<meta http-equiv="refresh" content="5">
Change "5" to whatever number of seconds you prefer.
If you want to refresh only when things have changed, you'd need to get some Javascript involved on the client - e.g. an Ajax request to a URL that asks if there's new data since page load, and refreshes if necessary.
来源:https://stackoverflow.com/questions/16243296/forece-refresh-client-page-from-server-side