標籤:

Swoole的簡單運用

前言

我們使用PHP開發WEB應用基本都是使用傳統的LAMP/LNMP模式來提供HTTP服務,這種模式一般是同步且堵塞的,若我們想使用PHP開發一些高級的特性(例如:非同步,非堵塞,網路伺服器等),那麼Swoole無疑是最佳的選擇,那什麼是Swoole呢?

PHP的非同步、並行、高性能網路通信引擎,使用純C語言編寫,提供了PHP語言的非同步多線程伺服器,非同步TCP/UDP網路客戶端,非同步MySQL,非同步Redis,資料庫連接池,AsyncTask,消息隊列,毫秒定時器,非同步文件讀寫,非同步DNS查詢。 Swoole內置了Http/WebSocket伺服器端/客戶端、Http2.0伺服器端/客戶端。

簡單的來說,Swoole是一個PHP擴展,實現了網路層的很多功能,應用場景非常廣,下面列舉幾個例子簡單介紹一下Swoole的應用。

安裝

按照官方文檔進行安裝:Swoole官網,安裝完後使用命令:

php -m

查看是否安裝成功。注意:Swoole從2.0版本開始支持了內置協程,需使用PHP7。

基於TCP的郵件伺服器

使用Swoole提供TCP服務,非同步任務發送郵件。

郵件功能:

PHPMailer

PHP主代碼:

<?php$object = new MailServer();$setting = [ log_file => swoole.log, worker_num => 4, // 4個工作進程 task_worker_num => 10, // 10個任務進程];$server = new swoole_server("127.0.0.1", 9501);$server->set($setting);$server->on(WorkerStart, array($object, onWorkerStart));$server->on(Connect, array($object, onConnect));$server->on(Receive, array($object, onReceive));$server->on(Close, array($object, onClose));$server->on(Task, array($object, onTask));$server->on(Finish, array($object, onFinish));$server->start();class MailServer{ /** @var Mail */ private $handle; public function __construct() { require Mail.php; // PHPMailer郵件服務類 } public function onWorkerStart($server, $workerId) { $mailConfig = require MailConfig.php; // 發件人信息,重啟時會重新載入配置文件 $this->handle = new Mail($mailConfig); } public function onConnect($server, $fd, $reactorId) { } public function onReceive($server, $fd, $reactorId, $data) { $return = []; $dataArr = json_decode($data, true); if (empty($dataArr) || empty($dataArr[address]) || empty($dataArr[subject]) || empty($dataArr[body])) { $return[code] = -1; $return[msg] = 參數不能為空; } else { // 參數校驗成功 $server->task($data); // 投遞一個任務 $return[code] = 0; $return[msg] = 投遞任務成功; } $server->send($fd, json_encode($return)); } public function onTask($server, $taskId, $workerId, $data) { $data = json_decode($data, true); $this->handle->send($data[address], $data[subject], $data[body]); // 發送郵件 } public function onFinish($server, $task_id, $data) { } public function onClose($server, $fd, $reactorId) { }}

發件人信息配置:

<?php// 郵件發送人信息配置return [ host => smtp.qq.com, port => 465, fromName => Mr.litt, username => 137057181@qq.com, password => ,];

PHPMailer郵件服務類:

<?phprequire vendor/phpmailer/phpmailer/src/Exception.php;require vendor/phpmailer/phpmailer/src/PHPMailer.php;require vendor/phpmailer/phpmailer/src/SMTP.php;use PHPMailerPHPMailerPHPMailer;class Mail{ private $host; private $port; private $fromName; private $username; private $password; public function __construct($config) { !empty($config[host]) && $this->host = $config[host]; !empty($config[port]) && $this->port = $config[port]; !empty($config[fromName]) && $this->fromName = $config[fromName]; !empty($config[username]) && $this->username = $config[username]; !empty($config[password]) && $this->password = $config[password]; if (empty($this->host) || empty($this->port) || empty($this->fromName) || empty($this->username) || empty($this->password)) { throw new Exception(發件人信息錯誤); } } public function send($address, $subject, $body) { if (empty($address) || empty($subject) || empty($body)) { throw new Exception(收件人信息錯誤); } // 實例化PHPMailer核心類 $mail = new PHPMailer(); // 是否啟用smtp的debug進行調試 開發環境建議開啟 生產環境注釋掉即可 默認關閉debug調試模式 $mail->SMTPDebug = 0; // 使用smtp鑒權方式發送郵件 $mail->isSMTP(); // smtp需要鑒權 這個必須是true $mail->SMTPAuth = true; // 鏈接郵箱的伺服器地址 $mail->Host = $this->host; // 設置使用ssl加密方式登錄鑒權 $mail->SMTPSecure = ssl; // 設置ssl連接smtp伺服器的遠程伺服器埠號 $mail->Port = $this->port; // 設置發送的郵件的編碼 $mail->CharSet = UTF-8; // 設置發件人昵稱 顯示在收件人郵件的發件人郵箱地址前的發件人姓名 $mail->FromName = $this->fromName; // smtp登錄的賬號 QQ郵箱即可 $mail->Username = $this->username; // smtp登錄的密碼 使用生成的授權碼 $mail->Password = $this->password; // 設置發件人郵箱地址 同登錄賬號 $mail->From = $this->username; // 郵件正文是否為html編碼 注意此處是一個方法 $mail->isHTML(true); // 設置收件人郵箱地址 $mail->addAddress($address); // 添加多個收件人 則多次調用方法即可 //$mail->addAddress(87654321@163.com); // 添加該郵件的主題 $mail->Subject = $subject; // 添加郵件正文 $mail->Body = $body; // 為該郵件添加附件 //$mail->addAttachment(./example.pdf); // 發送郵件 返回狀態 $status = $mail->send(); return $status; }}

注意事項:

  1. 修改發件人信息後,只需重啟task_worker就生效,命令 kill -USER1 主進程PID。
  2. TCP客戶端可使用swoole_client類來模擬。
  3. 簡訊、推送等非同步任務同樣適用於此場景。

基於WebSocket多房間聊天功能

使用Swoole提供WebSocket服務,使用Redis保存房間人員信息。

PHP主代碼:

<?php$object = new ChatServer();$setting = [ log_file => swoole_ws.log, worker_num => 4, // 4個工作進程];$ws = new swoole_websocket_server("127.0.0.1", 9502);$ws->set($setting);$ws->on(WorkerStart, array($object, onWorkerStart));$ws->on(open, array($object, onOpen));$ws->on(message, array($object, onMessage));$ws->on(close, array($object, onClose));$ws->start();class ChatServer{ /** @var Redis */ private $redis; public function __construct() { echo "啟動前清理數據
"; $redis = new Redis(); $redis->connect(127.0.0.1, 6379); if ($redis->ping() != +PONG) { echo "redis連接失敗
";exit; } $delKeys = $redis->keys(fd_*); foreach ($delKeys as $key) { $redis->del($key); } $delKeys = $redis->keys(roomId_*); foreach ($delKeys as $key) { $redis->del($key); } } public function onWorkerStart($ws, $workerId) { $redis = new Redis(); $redis->connect(127.0.0.1, 6379); if ($redis->ping() != +PONG) { echo "redis連接失敗
"; } $this->redis = $redis; } public function onOpen($ws, $request) { echo "fd:{$request->fd} is open
"; if (empty($request->get[roomId]) || empty($request->get[nick])) { $status = fail; } else { //建立身份關聯 $this->redis->hSet("fd_".$request->fd, roomId, $request->get[roomId]); $this->redis->hSet("fd_".$request->fd, nick, $request->get[nick]); $this->redis->sAdd("roomId_".$request->get[roomId], $request->fd); $status = success; } $sendData = [ cmd => open, data => [ status => $status ] ]; $ws->push($request->fd, json_encode($sendData)); } public function onMessage($ws, $frame) { echo "fd:[$frame->fd}, Message: {$frame->data}
"; if (!empty($frame->data)) { $fdInfo = $this->redis->hGetAll("fd_".$frame->fd); if (!empty($fdInfo[nick]) && !empty($fdInfo[roomId])) { $sendData = [ cmd => ReceiveMessage, data => [ nick => $fdInfo[nick], msg => $frame->data, ] ]; $fdArr = $this->redis->sMembers("roomId_".$fdInfo[roomId]); foreach ($fdArr as $fd) { $ws->push($fd, json_encode($sendData)); } } } } public function onClose($ws, $fd, $reactorId) { echo "fd:{$fd} is closed
"; //刪除fd身份數據並在房間內移動該fd $fdInfo = $this->redis->hGetAll("fd_".$fd); if (!empty($fdInfo[roomId])) { $this->redis->sRem("roomId_".$fdInfo[roomId], $fd); } $this->redis->del("fd_".$fd); }}

注意事項:

1.Worker進程之間不能共享變數,這裡使用Redis來共享數據。

2.Worker進程不能共用同一個Redis客戶端,需要放到onWorkerStart中實例化。

3.客戶端可使用JS內置等WebSokcet客戶端,非同步的PHP程序可使用SwooleHttpClient,同步可以使用swoole/framework提供的同步WebSocket客戶端。

基於HTTP的簡易框架

使用Swoole提供HTTP服務,模擬官方Swoole框架實現一個簡易框架。

PHP主代碼:

<?php$object = new AppServer();$setting = [ log_file => swoole_http.log, worker_num => 4, // 4個工作進程];$server = new swoole_http_server("127.0.0.1", 9503);$server->set($setting);$server->on(request, array($object, onRequest));$server->on(close, array($object, onClose));$server->start();/** * Class AppServer * @property swoole_http_request $request * @property swoole_http_response $response * @property PDO $db * @property libSession $session */class AppServer{ private $module = []; /** @var AppServer */ private static $instance; public static function getInstance() { return self::$instance; } public function __construct() { $baseControllerFile = __DIR__ ./controller/Base.php; require_once "$baseControllerFile"; } /** * @param swoole_http_request $request * @param swoole_http_response $response */ public function onRequest($request, $response) { $this->module[request] = $request; $this->module[response] = $response; self::$instance = $this; list($controllerName, $methodName) = $this->route($request); empty($controllerName) && $controllerName = index; empty($methodName) && $methodName = index; try { $controllerClass = "\controller\" . ucfirst($controllerName); $controllerFile = __DIR__ . "/controller/" . ucfirst($controllerName) . ".php"; if (!class_exists($controllerClass, false)) { if (!is_file($controllerFile)) { throw new Exception(控制器不存在); } require_once "$controllerFile"; } $controller = new $controllerClass($this); if (!method_exists($controller, $methodName)) { throw new Exception(控制器方法不存在); } ob_start(); $return = $controller->$methodName(); $return .= ob_get_contents(); ob_end_clean(); $this->session->end(); $response->end($return); } catch (Exception $e) { $response->status(500); $response->end($e->getMessage()); } } private function route($request) { $pathInfo = explode(/, $request->server[path_info]); return [$pathInfo[1], $pathInfo[2]]; } public function onClose($server, $fd, $reactorId) { } public function __get($name) { if (!in_array($name, array(request, response, db, session))) { return null; } if (empty($this->module[$name])) { $moduleClass = "\lib\" . ucfirst($name); $moduleFile = __DIR__ . /lib/ . ucfirst($name) . ".php"; if (is_file($moduleFile)) { require_once "$moduleFile"; $object = new $moduleClass; $this->module[$name] = $object; } } return $this->module[$name]; }}

使用header和setCooike示例:

<?phpnamespace controller;class Http extends Base{ public function header() { //發送Http狀態碼,如500, 404等等 $this->response->status(302); //使用此函數代替PHP的header函數 $this->response->header(Location, http://www.baidu.com/); } public function cookie() { $this->response->cookie(http_cookie,http_cookie_value); }}

Session實現:

<?phpnamespace lib;class Session{ private $sessionId; private $cookieKey; private $storeDir; private $file; private $isStart; public function __construct() { $this->cookieKey = PHPSESSID; $this->storeDir = tmp/; $this->isStart = false; } public function start() { $this->isStart = true; $appServer = AppServer::getInstance(); $request = $appServer->request; $response = $appServer->response; $sessionId = $request->cookie[$this->cookieKey]; if (empty($sessionId)){ $sessionId = uniqid(); $response->cookie($this->cookieKey, $sessionId); } $this->sessionId = $sessionId; $storeFile = $this->storeDir . $sessionId; if (!is_file($storeFile)) { touch($storeFile); } $session = $this->get($storeFile); $_SESSION = $session; } public function end() { $this->save(); } public function commit() { $this->save(); } private function save() { if ($this->isStart) { $data = json_encode($_SESSION); ftruncate($this->file, 0); if ($data) { rewind($this->file); fwrite($this->file, $data); } flock($this->file, LOCK_UN); fclose($this->file); } } private function get($fileName) { $this->file = fopen($fileName, c+b); if(flock($this->file, LOCK_EX | LOCK_NB)) { $data = []; clearstatcache(); if (filesize($fileName) > 0) { $data = fread($this->file, filesize($fileName)); $data = json_decode($data, true); } return $data; } }}

注意事項:

  1. 使用Redis/MySQL等客戶端理應使用線程池,參照官方Swoole框架。
  2. Swoole是在執行PHP文件這一階段進行接管,因此header,setCooike,seesion_start不可用,header和setCooike可用$response變數實現,Session可自行實現。
  3. 推薦查看官方框架:Swoole框架。

上述demo可以戳這裡:demo

總結

Swoole的應用遠不如此,Swoole就像打開了PHP世界的新大門,我覺得每一位PHPer都應該學習和掌握Swoole的用法,能學到很多使用LAMP/LNMP模式時未涉及到的知識點。


推薦閱讀:

目前來說在網站架構方面採用nobackend這種方案構建是否真的可行?
PHP自由職業者靠譜嗎?或者說是大學在校生,靠接一些項目來做賺錢養活自己。?
虛驚一場?PHPMailer漏洞雞肋無比
php為什麼弄點號連接字元串?
仿照TP3.2製作MVC框架(一) 文檔簡介

TAG:PHP | Swoole |