Real-time update of client data using Php+swoole (i), swooleclient
If you want to update a list in real time, the traditional approach is to use a polling method. Take the web as an example, through Ajax timed request server and then get the data displayed on the page. This approach is simple, and the downside is wasting resources.
HTTP1.1 adds support for websocket so that the passive display can be turned into an active notification. That is, through the WebSocket and the server to maintain a persistent link, once the data changes, the server notifies the client that the data is updated, and then refresh, and so on. This eliminates a lot of unnecessary passive requests and saves server resources.
To implement a Webscoket program, you first need to use a browser that supports HTML5
This achieves a client, but it is far from over. The above code is simply the basic action of connecting, talking, and closing. If you want to communicate with the server, you must have a more specific plan. For example, when a message is received to determine the type for further operation.
Server: Swoole is used here for PHP server websocket development, using Swoole for PHP websocket development is very simple, and it also supports httpserver support.
$server = new Swoole_websocket_server ("0.0.0.0", 8888), $server->on (' Open ', function (Swoole_websocket_server $ Server, $request) {echo "Server:handshake success with fd{$request->fd}\n";}); $server->on (' message ', function (Swoole_websocket_server $server, $frame) {echo "receive from {$frame->fd}:{$ frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n "; $server->push ($frame->fd," This is (server ");}); $server->on (' Close ', function ($ser, $FD) {echo "Client {$FD} closed\n";}); $server->start ();
Swoole is an extension of PHP, the installation method can be referenced here: PHP installation Swoole extension method
This article first wrote here, the next article will write some more specific operations, interested friends please continue to focus on this site. Thank you!
Articles you may be interested in:
- Extending the PHP websocket example with Swoole
- PHP Framework swoole Timer feature Analysis
- PHP Asynchronous multithreaded Swoole Usage example
- How to install Swoole extensions in PHP
- Swoole-1.7.22 version released to fix PHP7 related issues
http://www.bkjia.com/PHPjc/1089583.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089583.html techarticle using Php+swoole to update client data in real time (i), swooleclient if you want to update a list in real time, the traditional approach is to use polling. Take the web as an example, via Ajax timing ...