Swoole first recognized, swoole first recognized
Official definition:
Swoole: redefine PHP
PHP's asynchronous, parallel, and high-performance network communication engine, written in pure C language, provides PHP-language asynchronous multi-thread servers, asynchronous TCP/UDP network clients, asynchronous MySQL, asynchronous Redis, database Connection Pool, AsyncTask, message queue, millisecond timer, asynchronous file read/write, asynchronous DNS query. Swoole has built-in Http/WebSocket server/client and Http2.0 server.
Swoole can be widely used in the Internet, mobile communication, enterprise software, cloud computing, online games, Iot, IOV, Smart Home, and other fields. Using PHP + Swoole as the network communication framework can greatly improve the efficiency of enterprise it r & D teams and focus more on developing innovative products.
Swoole extension installation and case study Source: http://wiki.swoole.com/wiki/page/6.html
Simple case:
<? Phpclass Server {private $ serv; public function _ construct () {$ this-> serv = new swoole_server ("0.0.0.0", 9501 ); $ this-> serv-> set (array ('worker _ num' => 8, 'daemonize '=> false, 'max _ request' => 10000, 'dispatch _ mode' => 2, 'debug _ mode' => 1); $ this-> serv-> on ('start', array ($ this, 'onstart'); $ this-> serv-> on ('connect ', array ($ this, 'onconnect ')); $ this-> serv-> on ('receive ', array ($ this, 'onrecei Ve '); $ this-> serv-> on ('close', array ($ this, 'onclose ')); $ this-> serv-> start ();} public function onStart ($ serv) {echo "Start \ n";} public function onConnect ($ serv, $ fd, $ from_id) {$ serv-> send ($ fd, "Hello {$ fd }! ");} Public function onReceive (swoole_server $ serv, $ fd, $ from_id, $ data) {echo" Get Message From Client {$ fd }: {$ data} \ n ";} public function onClose ($ serv, $ fd, $ from_id) {echo" Client {$ fd} close connection \ n ";}} // start the server $ Server = new server ();
<? Phpclass Client {private $ client; public function _ construct () {$ this-> client = new swoole_client (SWOOLE_SOCK_TCP);} public function connect () {if (! $ This-> client-> connect ("127.0.0.1", 9501, 1) {echo "Error: {$ fp-> errMsg} [{$ fp-> errCode}] \ n ";}$ message = $ this-> client-> recv (); echo "Get Message From Server: {$ message} \ n"; fwrite (STDOUT, "Enter the Message:"); $ msg = trim (fgets (STDIN )); $ this-> client-> send ($ msg) ;}$ client = new Client (); $ client-> connect ();
Open two terminal inputs: php server. php client. php to see the effect!