生活随笔
收集整理的這篇文章主要介紹了
jsp项目开发案例_Laravel中使用swoole项目实战开发案例一 (建立swoole和前端通信)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Laravel中使用swoole項(xiàng)目實(shí)戰(zhàn)開發(fā)案例二(后端主動(dòng)分場(chǎng)景給界面推送消息)
工欲善其事,必先利其器。在正式開發(fā)之前我們檢查好需要安裝的拓展,不要開發(fā)中發(fā)現(xiàn)這些問題,打斷思路影響我們的開發(fā)效率。
- 安裝 swoole 拓展包
- 安裝 redis 拓展包
- 安裝 laravel5.5 版本以上
如果你還不會(huì)用swoole就out了
如果喜歡我的文章,想與一群資深開發(fā)者一起交流學(xué)習(xí)的話,獲取更多相關(guān)大廠面試咨詢和指導(dǎo),關(guān)注我或點(diǎn)擊此處
2 Laravel 生成命令行
php artisan make:command SwooleDemoclass SwooleDemo extends Command{protected $signature = 'swoole:demo';protected $description = '這是關(guān)于swoole的一個(gè)測(cè)試demo';public function __construct(){ parent::__construct();}public function handle(){ $this->line("hello world");}}
我們分別運(yùn)行 php artisan 指令和 php artisan swoole:demo 會(huì)看到關(guān)于這個(gè)命令的說明,和輸出 hello world。(laravel 命令行用法詳解)
本課程為swoole入門教程,通過從swoole的安裝講到swoole-tcp、同步客戶端、異步客戶端、udp到服務(wù)端客戶端等技能,同時(shí)每一小結(jié)理論配套相關(guān)商業(yè)項(xiàng)目實(shí)戰(zhàn)案例,增加學(xué)習(xí)效果,達(dá)到熟練掌握并使用。
喜歡我的文章可以找我要進(jìn)階資料,助力你達(dá)到30K
3 命令行邏輯代碼
- 編寫一個(gè)最基礎(chǔ)的 swoole 命令行邏輯代碼
<?phpnamespace AppConsoleCommands;use IlluminateConsoleCommand;use IlluminateSupportFacadesRedis;class SwooleDemo extends Command{ // 命令名稱 protected $signature = 'swoole:demo'; // 命令說明 protected $description = '這是關(guān)于swoole websocket的一個(gè)測(cè)試demo'; // swoole websocket服務(wù) private static $server = null; public function __construct() { parent::__construct(); } // 入口 public function handle() { $this->redis = Redis::connection('websocket'); $server = self::getWebSocketServer(); $server->on('open',[$this,'onOpen']); $server->on('message', [$this, 'onMessage']); $server->on('close', [$this, 'onClose']); $server->on('request', [$this, 'onRequest']); $this->line("swoole服務(wù)啟動(dòng)成功 ..."); $server->start(); } // 獲取服務(wù) public static function getWebSocketServer() { if (!(self::$server instanceof swoole_websocket_server)) { self::setWebSocketServer(); } return self::$server; } // 服務(wù)處始設(shè)置 protected static function setWebSocketServer():void { self::$server = new swoole_websocket_server("0.0.0.0", 9502); self::$server->set([ 'worker_num' => 1, 'heartbeat_check_interval' => 60, // 60秒檢測(cè)一次 'heartbeat_idle_time' => 121, // 121秒沒活動(dòng)的 ]); } // 打開swoole websocket服務(wù)回調(diào)代碼 public function onOpen($server, $request) { if ($this->checkAccess($server, $request)) { self::$server->push($request->fd,"打開swoole服務(wù)成功!"); } } // 給swoole websocket 發(fā)送消息回調(diào)代碼 public function onMessage($server, $frame) { } // http請(qǐng)求swoole websocket 回調(diào)代碼 public function onRequest($request,$response) { } // websocket 關(guān)閉回調(diào)代碼 public function onClose($serv,$fd) { $this->line("客戶端 {$fd} 關(guān)閉"); } // 校驗(yàn)客戶端連接的合法性,無效的連接不允許連接 public function checkAccess($server, $request):bool { $bRes = true; if (!isset($request->get) || !isset($request->get['token'])) { self::$server->close($request->fd); $this->line("接口驗(yàn)證字段不全"); $bRes = false; } else if ($request->get['token'] !== "123456") { $this->line("接口驗(yàn)證錯(cuò)誤"); $bRes = false; } return $bRes; } // 啟動(dòng)websocket服務(wù) public function start() { self::$server->start(); }}
編寫 websoket js 代碼
swoole測(cè)試
這是一個(gè)測(cè)試
總結(jié)
以上是生活随笔為你收集整理的jsp项目开发案例_Laravel中使用swoole项目实战开发案例一 (建立swoole和前端通信)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。