Rt: I want to use websocket instead of polling. How can I configure websocket server rt?
I want to use websocket instead of polling
How can I configure the websocket server?
Reply content:
Rt
I want to use websocket instead of polling
How can I configure the websocket server?
PHP itself does not provide WebSocket-related functions. You need to use third-party extensions. For example, fengge's PECL extension Swoole developed by C, which directly encapsulates WebSocket Server, for example, the official website example:
Http://www.swoole.com/
on('Open', function($server, $req) { echo "connection open: ".$req->fd;});$serv->on('Message', function($server, $frame) { echo "message: ".$frame->data; $server->push($frame->fd, json_encode(["hello", "world"]));});$serv->on('Close', function($server, $fd) { echo "connection close: ".$fd;});$serv->start();
PHPWebIM is an official WebSocket webpage instant chat tool developed by Swoole based on PHP Swoole extension and Swoole Framework. PHPWebIM supports WebSocket + Comet protocols and can be used in all types of browsers, including IE.
Https://github.com/matyhtf/PHPWebIM
Demo: http://webim.swoole.com/
After compiling and installing PHP, run PHP_PREFIX/bin/pecl install swoole to install the Swoole extension, because the Swoole extension has been included in the official php pecl repository and supports PHP7:
Https://pecl.php.net/package/swoole