官方定義:
Swoole:重新定義PHP
PHP的非同步、並行、高效能網路通訊引擎,使用純C語言編寫,提供了PHP語言的非同步多執行緒伺服器,非同步TCP/UDP網路客戶端,異步MySQL,非同步Redis,資料庫連線池,AsyncTask,訊息佇列,毫秒定時器,非同步檔案讀寫,非同步DNS查詢。 Swoole內建了Http/WebSocket伺服器端/客戶端、Http2.0伺服器端。
Swoole可廣泛應用於互聯網、行動通訊、企業軟體、雲端運算、網路遊戲、物聯網、車聯網、智慧家庭等領域。 使用PHP+Swoole作為網路通訊框架,可以讓企業IT研發團隊的效率大大提升,更專注於開發創新產品。
swoole 擴充安裝及案例來源:http://wiki.swoole.com/wiki/page/6.html
簡單案例:
<?<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();
分別開啟兩個終端輸入:php phpserver. php client.php 即可看到效果!
以上就介紹了Swoole 初識,包括了wool,ole方面的內容,希望對PHP教程有興趣的朋友有所幫助。