摘要:進程可以使用函數(shù)向進程投遞新的任務(wù)。當(dāng)前的進程在調(diào)用回調(diào)函數(shù)時會將進程狀態(tài)切換為忙碌,這時將不再接收新的,當(dāng)函數(shù)返回時會將進程狀態(tài)切換為空閑然后繼續(xù)接收新的。當(dāng)進程投遞的任務(wù)在中完成時,進程會通過方法將任務(wù)處理的結(jié)果發(fā)送給進程。
swoole——從入門到放棄(一) 一、swoole的源碼包安裝
下載swoole源碼:git clone https://gitee.com/swoole/swoole.git
通過phpize(擴展php擴展模塊,建立php外掛模塊):
cd swoole
執(zhí)行: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開啟的端口號)
學(xué)習(xí)swoole需要去翻閱文檔,swoole文檔1.通過swoole創(chuàng)建一個最簡單的tcp服務(wù)
tcp服務(wù)端(tcp_server.php)
//創(chuàng)建Server對象,監(jiān)聽 127.0.0.1:9501端口 $serv = new swoole_server("127.0.0.1", 9501); $serv->set([ "worker_num" => 4, // worker進程數(shù),cpu 1-4倍 "max_request" => 100, ]); /** * 監(jiān)聽連接進入事件 * $fd 客戶端連接服務(wù)端的唯一標(biāo)識 * $reactor_id 線程id */ $serv->on("connect", function ($serv, $fd, $reactor_id) { echo "Client: {$fd} - {$reactor_id} - Connect. "; }); //監(jiān)聽數(shù)據(jù)接收事件 $serv->on("receive", function ($serv, $fd, $reactor_id, $data) { $serv->send($fd, "Server: ".$data); }); //監(jiān)聽連接關(guān)閉事件 $serv->on("close", function ($serv, $fd) { echo "Client: Close. "; }); //啟動服務(wù)器 $serv->start();
tcp客戶端(tcp_client.php)
// 創(chuàng)建tcp客戶端 $client = new swoole_client(SWOOLE_SOCK_TCP); // 連接tcp服務(wù)端 if (!$client->connect("127.0.0.1", 9501)) { echo "連接失敗"; exit; } // php cli fwrite(STDOUT, "請輸入:"); $msg = trim(fgets(STDIN)); // 發(fā)送消息給tcp服務(wù)端 if (!$client->send($msg)) { echo "發(fā)送消息失敗"; exit; } // 接收 $result = $client->recv(); echo $result;2.拓展:php的四種回調(diào)
匿名函數(shù)
$server->on("Request", function ($req, $resp) { echo "hello world"; });
類靜態(tài)方法
class A { static function test($req, $resp) { echo "hello world"; } } $server->on("Request", "A::Test"); $server->on("Request", array("A", "Test"));
函數(shù)
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
// 監(jiān)聽所有地址和9501端口 $http = new swoole_http_server("0.0.0.0", 9501); // 動靜分離配置 $http->set([ // 開啟靜態(tài)請求 "enable_static_handler" => true, // 靜態(tài)資源目錄 "document_root" => "/opt/app/code1/", ]); $http->on("request", function ($request, $response) { // 獲取get請求的參數(shù) $param = json_encode($request->get); // 設(shè)置cookie $response->cookie("name", "ronaldo", time() + 1800); // 輸出到頁面 $response->end("Hello Swoole - {$param}
"); }); // 開啟http服務(wù) $http->start();
websocket服務(wù)端(websocket_server.php)
// 監(jiān)聽所有地址和9502端口 $server = new swoole_websocket_server("0.0.0.0", 9502); // 動靜分離配置 $server->set([ // 開啟靜態(tài)請求 "enable_static_handler" => true, // 靜態(tài)資源目錄 "document_root" => "/opt/app/swoole/websocket", ]); $server->on("open", function ($server, $request) { echo "server:handshake success with fd - {$request->fd} "; }); $server->on("message", function ($server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish} "; $server->push($frame->fd, "this is server"); }); $server->on("close", function ($server, $fd) { echo "client - {$fd} - close "; }); $server->start();
websocket客戶端 (websockt_client.html)
// 創(chuàng)建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屬性,接收服務(wù)端返回的數(shù)據(jù) 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(); } // 監(jiān)聽websocket連接事件 function onOpen($server, $request) { echo "server: handshake success with fd{$request->fd} "; } // 監(jiān)聽websocket消息接收事件 function onMessage($server, $frame) { echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish} "; $server->push($frame->fd, "this is server"); } // 監(jiān)聽客戶端關(guān)閉事件 function onClose($server, $fd) { echo "Client:{$fd} closes "; } }
onTask:在task_worker進程內(nèi)被調(diào)用。worker進程可以使用swoole_server_task函數(shù)向task_worker進程投遞新的任務(wù)。當(dāng)前的Task進程在調(diào)用onTask回調(diào)函數(shù)時會將進程狀態(tài)切換為忙碌,這時將不再接收新的Task,當(dāng)onTask函數(shù)返回時會將進程狀態(tài)切換為空閑然后繼續(xù)接收新的Task。
onFinish:當(dāng)worker進程投遞的任務(wù)在task_worker中完成時,task進程會通過swoole_server->finish()方法將任務(wù)處理的結(jié)果發(fā)送給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} "; } public function onMessage($server, $frame) { echo "receive from {$frame->fd}:{$frame->data} "; // 需要投遞的任務(wù)數(shù)據(jù) $data = [ "fd" => $frame->fd, "msg" => "task", ]; $server->task($data); $server->push($frame->fd, "this is server"); } // 處理投遞的任務(wù)方法,非阻塞 public function onTask($server, $task_id, $worker_id, $data) { print_r($data); // 模擬大量數(shù)據(jù)的操作 sleep(10); return "task_finish"; } // 投遞任務(wù)處理完畢調(diào)用的方法 public function onFinish($server, $task_id, $data) { echo "task_id:{$task_id} "; echo "task finish success:{$data} "; } public function onClose($server, $fd) { echo "Client:close"; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/30762.html
摘要:從入門到放棄三一進程子進程創(chuàng)建成功后要執(zhí)行的函數(shù)重定向子進程的標(biāo)準(zhǔn)輸入和輸出。默認為阻塞讀取。是否創(chuàng)建管道,啟用后,此選項將忽略用戶參數(shù),強制為。 swoole——從入門到放棄(三) 一、進程 swoole_process SwooleProcess swoole_process::__construct(callable $function, $redirect_stdin...
摘要:從入門到放棄三一進程子進程創(chuàng)建成功后要執(zhí)行的函數(shù)重定向子進程的標(biāo)準(zhǔn)輸入和輸出。默認為阻塞讀取。是否創(chuàng)建管道,啟用后,此選項將忽略用戶參數(shù),強制為。 swoole——從入門到放棄(三) 一、進程 swoole_process SwooleProcess swoole_process::__construct(callable $function, $redirect_stdin...
摘要:從入門到放棄二一異步毫秒定時器設(shè)置一個間隔時鐘定時器,與定時器不同的是定時器會持續(xù)觸發(fā),直到調(diào)用清除。是一次性函數(shù),執(zhí)行完成后就會銷毀最大不超過使用定時器來刪除定時器。 swoole——從入門到放棄(二) 一、異步毫秒定時器 swoole_timer_tick:設(shè)置一個間隔時鐘定時器,與after定時器不同的是tick定時器會持續(xù)觸發(fā),直到調(diào)用swoole_timer_clear清...
摘要:從入門到放棄二一異步毫秒定時器設(shè)置一個間隔時鐘定時器,與定時器不同的是定時器會持續(xù)觸發(fā),直到調(diào)用清除。是一次性函數(shù),執(zhí)行完成后就會銷毀最大不超過使用定時器來刪除定時器。 swoole——從入門到放棄(二) 一、異步毫秒定時器 swoole_timer_tick:設(shè)置一個間隔時鐘定時器,與after定時器不同的是tick定時器會持續(xù)觸發(fā),直到調(diào)用swoole_timer_clear清...
閱讀 2591·2021-11-18 10:02
閱讀 2627·2021-11-15 11:38
閱讀 3697·2021-11-12 10:36
閱讀 695·2021-11-12 10:34
閱讀 2888·2021-10-21 09:38
閱讀 1478·2021-09-29 09:48
閱讀 1492·2021-09-29 09:34
閱讀 1088·2021-09-22 10:02