您好,登錄后才能下訂單哦!
小編給大家分享一下swoole的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
下載swoole源碼:git clone https://gitee.com/swoole/swoole.git
通過phpize(擴展php擴展模塊,建立php外掛模塊):
cd swoole
執行:your/phpize/path
./configure --with-php-config=your/php/path/bin/php-config
make && make install
可以看到swoole.so的位置
我的位置是:/opt/soft/php/lib/php/extensions/no-debug-non-zts-20170718/
配置php.ini
添加extension=swoole.so
通過php -m
命令,可以看到php的擴展模塊
檢測swoole安裝成功并且php支持swoole
cd your/swoole/path/examples/server
php echo.php
(如果進程被阻塞,則說明成功)
netstat -anp | grep 9501
(查看swoole開啟的端口號)
學習swoole需要去翻閱文檔, swoole文檔
tcp服務端(tcp_server.php)
//創建Server對象,監聽 127.0.0.1:9501端口 $serv = new swoole_server("127.0.0.1", 9501); $serv->set([ 'worker_num' => 4, // worker進程數,cpu 1-4倍 'max_request' => 100, ]); /** * 監聽連接進入事件 * $fd 客戶端連接服務端的唯一標識 * $reactor_id 線程id */ $serv->on('connect', function ($serv, $fd, $reactor_id) { echo "Client: {$fd} - {$reactor_id} - Connect.\n"; }); //監聽數據接收事件 $serv->on('receive', function ($serv, $fd, $reactor_id, $data) { $serv->send($fd, "Server: ".$data); }); //監聽連接關閉事件 $serv->on('close', function ($serv, $fd) { echo "Client: Close.\n"; }); //啟動服務器 $serv->start();
tcp客戶端(tcp_client.php)
// 創建tcp客戶端 $client = new swoole_client(SWOOLE_SOCK_TCP); // 連接tcp服務端 if (!$client->connect("127.0.0.1", 9501)) { echo '連接失敗'; exit; } // php cli fwrite(STDOUT, '請輸入:'); $msg = trim(fgets(STDIN)); // 發送消息給tcp服務端 if (!$client->send($msg)) { echo '發送消息失敗'; exit; } // 接收 $result = $client->recv(); echo $result;
匿名函數
$server->on('Request', function ($req, $resp) { echo "hello world"; });
類靜態方法
class A { static function test($req, $resp) { echo "hello world"; } } $server->on('Request', 'A::Test'); $server->on('Request', array('A', 'Test'));
函數
function my_onRequest($req, $resp) { echo "hello world"; } $server->on('Request', 'my_onRequest');
對象方法
class A { function test($req, $resp) { echo "hello world"; } } $object = new A(); $server->on('Request', array($object, 'test'));
小技巧:查看開啟的worker進程: ps aft | grep tcp_server.php
// 監聽所有地址和9501端口 $http = new swoole_http_server('0.0.0.0', 9501); // 動靜分離配置 $http->set([ // 開啟靜態請求 'enable_static_handler' => true, // 靜態資源目錄 'document_root' => '/opt/app/code1/', ]); $http->on('request', function ($request, $response) { // 獲取get請求的參數 $param = json_encode($request->get); // 設置cookie $response->cookie('name', 'ronaldo', time() + 1800); // 輸出到頁面 $response->end("<h2>Hello Swoole - {$param}</h2>"); }); // 開啟http服務 $http->start();
websocket服務端(websocket_server.php)
// 監聽所有地址和9502端口 $server = new swoole_websocket_server('0.0.0.0', 9502); // 動靜分離配置 $server->set([ // 開啟靜態請求 'enable_static_handler' => true, // 靜態資源目錄 'document_root' => '/opt/app/swoole/websocket', ]); $server->on('open', function ($server, $request) { echo "server:handshake success with fd - {$request->fd}\n"; }); $server->on('message', function ($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 ($server, $fd) { echo "client - {$fd} - close\n"; }); $server->start();
websocket客戶端 (websockt_client.html)
// 創建websocket實例 var websocketURL = "ws://www.rona1do.top:9502"; var websocket = new WebSocket(websocketURL); // 實例化對象的onopen屬性 websocket.onopen = function (ev) { websocket.send("hello-websocket"); console.log("connect-swoole-success"); } // 實例化對象的onmessage屬性,接收服務端返回的數據 websocket.onmessage = function (ev) { console.log("websockect-server-return-data:" + ev.data); } // close websocket.onclose = function (ev) { console.log("close"); }
class WebSocket { const HOST = '0.0.0.0'; const PORT = 9502; private $ws = null; function __construct() { $this->ws = new swoole_websocket_server(self::HOST, self::PORT); $this->ws->on('open', [$this, 'onOpen']); $this->ws->on('message', [$this, 'onMessage']); $this->ws->on('close', [$this, 'onClose']); $this->ws->start(); } // 監聽websocket連接事件 function onOpen($server, $request) { echo "server: handshake success with fd{$request->fd}\n"; } // 監聽websocket消息接收事件 function onMessage($server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $server->push($frame->fd, "this is server"); } // 監聽客戶端關閉事件 function onClose($server, $fd) { echo "Client:{$fd} closes\n"; } }
onTask:在task_worker進程內被調用。worker進程可以使用swoole_server_task函數向task_worker進程投遞新的任務。當前的Task進程在調用onTask回調函數時會將進程狀態切換為忙碌,這時將不再接收新的Task,當onTask函數返回時會將進程狀態切換為空閑然后繼續接收新的Task。
onFinish:當worker進程投遞的任務在task_worker中完成時,task進程會通過swoole_server->finish()方法將任務處理的結果發送給worker進程。
class Websocket { const HOST = '0.0.0.0'; const PORT = 9502; private $ws = null; public function __construct() { $this->ws = new swoole_websocket_server(self::HOST, self::PORT); $this->ws->set([ 'worker_num' => 2, 'task_worker_num' => 2, // 要想使用task必須要指明 ]); $this->ws->on('open', [$this, 'onOpen']); $this->ws->on('message', [$this, 'onMessage']); $this->ws->on('task', [$this, 'onTask']); $this->ws->on('finish', [$this, 'onFinish']); $this->ws->on('close', [$this, 'onClose']); $this->ws->start(); } public function onOpen($server, $request) { echo "server:handshake success with fd:{$request->fd}\n"; } public function onMessage($server, $frame) { echo "receive from {$frame->fd}:{$frame->data}\n"; // 需要投遞的任務數據 $data = [ 'fd' => $frame->fd, 'msg' => 'task', ]; $server->task($data); $server->push($frame->fd, 'this is server'); } // 處理投遞的任務方法,非阻塞 public function onTask($server, $task_id, $worker_id, $data) { print_r($data); // 模擬大量數據的操作 sleep(10); return "task_finish"; } // 投遞任務處理完畢調用的方法 public function onFinish($server, $task_id, $data) { echo "task_id:{$task_id}\n"; echo "task finish success:{$data}\n"; } public function onClose($server, $fd) { echo "Client:close"; } }
以上是“swoole的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。