Official definition:
Swoole: Redefines PHP
PHP's asynchronous, parallel, high-performance network communication engine, written in pure C language, provides an asynchronous multi-threaded server in PHP language, asynchronous TCP/UDP network client, asynchronous MySQL, asynchronous Redis, database connection pool, AsyncTask, message queue, millisecond timer, asynchronous file reading and writing, asynchronous DNS query. Swoole has built-in Http/WebSocket server/client and Http2.0 server.
Swoole can be widely used in the Internet, mobile communications, enterprise software, cloud computing, online games, Internet of Things, Internet of Vehicles, smart homes and other fields. Using PHP+Swoole as the network communication framework can greatly improve the efficiency of the enterprise IT R&D team and focus more on developing innovative products.
swoole extension installation and case source: http://wiki.swoole.com/wiki/page/6.html
Simple case:
<?<span>php </span><span>class</span><span> Server { </span><span>private</span><span>$serv</span><span>; </span><span>public</span><span>function</span><span> __construct() { </span><span>$this</span>->serv = <span>new</span> swoole_server("0.0.0.0", 9501<span>); </span><span>$this</span>->serv->set(<span>array</span><span>( </span>'worker_num' => 8, 'daemonize' => <span>false</span>, 'max_request' => 10000, 'dispatch_mode' => 2, 'debug_mode' => 1<span> )); </span><span>$this</span>->serv->on('Start', <span>array</span>(<span>$this</span>, 'onStart'<span>)); </span><span>$this</span>->serv->on('Connect', <span>array</span>(<span>$this</span>, 'onConnect'<span>)); </span><span>$this</span>->serv->on('Receive', <span>array</span>(<span>$this</span>, 'onReceive'<span>)); </span><span>$this</span>->serv->on('Close', <span>array</span>(<span>$this</span>, 'onClose'<span>)); </span><span>$this</span>->serv-><span>start(); } </span><span>public</span><span>function</span> onStart(<span>$serv</span><span>) { </span><span>echo</span> "Start\n"<span>; } </span><span>public</span><span>function</span> onConnect(<span>$serv</span>, <span>$fd</span>, <span>$from_id</span><span>) { </span><span>$serv</span>->send(<span>$fd</span>, "Hello {<span>$fd</span>}!"<span>); } </span><span>public</span><span>function</span> onReceive(swoole_server <span>$serv</span>, <span>$fd</span>, <span>$from_id</span>, <span>$data</span><span>) { </span><span>echo</span> "Get Message From Client {<span>$fd</span>}:{<span>$data</span>}\n"<span>; } </span><span>public</span><span>function</span> onClose(<span>$serv</span>, <span>$fd</span>, <span>$from_id</span><span>) { </span><span>echo</span> "Client {<span>$fd</span>} close connection\n"<span>; } } </span><span>//</span><span> 启动服务器</span><span>$server</span> = <span>new</span> Server();
<?<span>php </span><span>class</span><span> Client { </span><span>private</span><span>$client</span><span>; </span><span>public</span><span>function</span><span> __construct() { </span><span>$this</span>->client = <span>new</span><span> swoole_client(SWOOLE_SOCK_TCP); } </span><span>public</span><span>function</span><span> connect() { </span><span>if</span> (!<span>$this</span>->client->connect("127.0.0.1", 9501, 1<span>)) { </span><span>echo</span> "Error: {<span>$fp</span>->errMsg}[{<span>$fp</span>->errCode}]\n"<span>; } </span><span>$message</span> = <span>$this</span>->client-><span>recv(); </span><span>echo</span> "Get Message From Server:{<span>$message</span>}\n"<span>; </span><span>fwrite</span>(STDOUT, "请输入消息:"<span>); </span><span>$msg</span> = <span>trim</span>(<span>fgets</span><span>(STDIN)); </span><span>$this</span>->client->send(<span>$msg</span><span>); } } </span><span>$client</span> = <span>new</span><span> Client(); </span><span>$client</span>->connect();
Open two terminals respectively and enter: php server.php php client.php to see the effect!
The above introduces the first introduction to Swoole, including the content of wool and ole. I hope it will be helpful to friends who are interested in PHP tutorials.