環境:基于composer引入包和thinkphp6+https+workerman.
1.composer引入gatewayworker 使用phpstorm更方便(在composer.json中require中添加這些 phpstorm會自動引入) 或者使用命令行 composer require XXX
```
`"workerman/workerman"``:``"^4.0"``,`
`"workerman/gateway-worker"``:``"^3.0"``,`
`"workerman/gatewayclient"``:``"^3.0"``,`
```
0.編寫workerman啟動文件 workerman單獨部署的 與你的項目無關 public/workerman.php(這個用來測試workman啟動的-------------------)
```
`<?php`
`// +----------------------------------------------------------------------`
`// | ThinkPHP [ WE CAN DO IT JUST THINK ]`
`// +----------------------------------------------------------------------`
`// | Copyright (c) 2006-2019 http://thinkphp.cn All rights reserved.`
`// +----------------------------------------------------------------------`
`// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )`
`// +----------------------------------------------------------------------`
`// | Author: liu21st <liu21st@gmail.com>`
`// +----------------------------------------------------------------------`
`// [ wokerman啟動文件 和整個項目無關聯]`
`namespace``think;`
`use``Workerman\Worker;`
`require_once``__DIR__ .``'/../vendor/workerman/workerman/Autoloader.php'``;`
`// 創建一個Worker監聽2345端口,使用http協議通訊`
`$http_worker``=``new``Worker(``"http://0.0.0.0:2345"``);`
`// 啟動4個進程對外提供服務`
`$http_worker``->``count``= 4;`
`// 接收到瀏覽器發送的數據時回復hello world給瀏覽器`
`$http_worker``->onMessage =``function``(``$connection``,``$data``)`
`{`
`// 向瀏覽器發送hello world`
`//? $connection->send('hello world');`
`};`
`// 運行worker`
`Worker::runAll();`
```
2.利用命令創建workerman php think make:command workerman 并編寫文件app/command/Workerman.php
```
`<?php`
`//declare (strict_types=1);`
`namespace``app\command;`
`use``GatewayWorker\BusinessWorker;`
`use``GatewayWorker\Gateway;`
`use``GatewayWorker\Register;`
`use``think\console\Command;`
`use``think\console\Input;`
`use``think\console\input\Argument;`
`use``think\console\input\Option;`
`use``think\console\Output;`
`use``Workerman\Worker;`
`require_once``__DIR__ .``'/../../vendor/workerman/workerman/Autoloader.php'``;`
`class``Workerman``extends``Command`
`{`
`protected``function``configure()`
`{`
`// 指令配置`
`$this``->setName(``'workerman'``)`
`->setDescription(``'the workerman command'``);`
`}`
`protected``function``execute(Input``$input``, Output``$output``)`
`{`
`$this``->startGateWay();`
`$this``->startBusinessWorker();`
`$this``->startRegister();`
`Worker::runAll();`
`}`
`private``function``startBusinessWorker()`
`{`
`$worker``=``new``BusinessWorker();`
`$worker``->name =``'BusniessWorker'``;`
`$worker``->``count``= 1;`
`$worker``->registerAddress =``"127.0.0.1:1236"``;`
`$worker``->eventHandler = \app\workerman\Events::``class``;`
`}`
`private``function``startGateWay()`
`{`
`$gateway``=``new``Gateway(``"websocket://127.0.0.1:2346"``);`
`$gateway``->name =``'Gateway'``;`
`$gateway``->``count``= 1;`
`$gateway``->lanIp =``'127.0.0.1'``;`
`$gateway``->startPort = 2300;`
`$gateway``->pingInterval = 30;`
`$gateway``->pingNotResponseLimit = 0;`
`$gateway``->pingData =``'{"type":"heart"}'``;`
`$gateway``->registerAddress =``"127.0.0.1:1236"``;`
`}`
`private``function``startRegister()`
`{`
`new``Register(``'text://127.0.0.1:1236'``);`
`}`
`}`
```
3.定義workerman監聽調用方法 heart心跳檢測?
```
`public``function``workerman_message(``$admin_id``=``''``,``$content``=``''``){`
`Gateway::``$registerAddress``=``'0.0.0.0:1236'``;`
`// 向任意uid的網站頁面發送數據`
`$uid``=``$admin_id``;`
`$admin``= SystemAdmin::find(``$admin_id``);`
`$message``=``new``\stdClass();`
`$message``->type=``'send'``;`
`if``(``$uid``){`
`$message``->from=``$admin_id``??``''``;`
`$message``->from_name=``$admin``[``'username'``]??``'后臺消息'``;`
`$message``->from_avatar=``$admin``[``'head_img'``]??``'https://www.huixx.cn/upload/20200828/e68c50db7b27c784b13a13b87c8ffc71.png'``;`
`$message``->content=``$content``;`
`Gateway::sendToUid(``$uid``, json_encode(``$message``,JSON_UNESCAPED_UNICODE));`
`}``else``{`
`$message``->from_name=``'后臺提醒'``;`
`$message``->from_avatar=``'https://www.huixx.cn/upload/20200828/e68c50db7b27c784b13a13b87c8ffc71.png'``;`
`$message``->content=``$content``;`
`Gateway::sendToAll(json_encode(``$message``,JSON_UNESCAPED_UNICODE));`
`}`
`}`
```
4.前端開啟websocket監聽 加載html提示框?
```
`/**`
`* 與GatewayWorker建立websocket連接,域名和端口改為你實際的域名端口,`
`* 其中端口為Gateway端口,即start_gateway.php指定的端口。`
`* start_gateway.php 中需要指定websocket協議,像這樣`
`* $gateway = new Gateway(websocket://0.0.0.0:7272);`
`*/`
`Notification.requestPermission();`
`let``protocol = location.protocol ===``'https:'`
`?``'wss://test.huixx.cn/wss/'`
`:``'ws://0.0.0.0:2346'``;`
`ws =``new``WebSocket(protocol);`
`console.log(``'websocket啟動'``)`
`// 服務端主動推送消息時會觸發這里的onmessage`
`ws.onmessage =``function``(e) {`
`// json數據轉換成js對象`
`var``data = eval(``"("``+ e.data +``")"``);`
`var``type = data.type ? data.type :``""``;`
`switch``(type) {`
`case``0:`
`break``;`
`case``"connect"``:`
`console.log(``'連婕socket成功!'``);`
`/*進行id綁定*/`
`var``url =``'/workerman/bind'``;`
`var``data_post = {`
`client_id: data.id,`
`};`
`$.post(url, data_post,``function``(re) {`
`var``r = /^[0-9]*$/;`
`if``(r.test(re)) {`
`console.log(``'綁定uid:'``+ re +``"成功"``);`
`}`
`},``'json'``);`
`//查詢未接收消息數 并且放置`
`break``;`
`case``"heart"``:`
`console.log(``'心跳檢測正常'``);`
`break``;`
`/*接受到消息的處理*/`
`case``"send"``:`
`//更新在線聊天按鈕`
`console.log(data);`
`layer.msg(data.content)`
`var``domain =``'https://'``+ document.domain;`
`var``icon =``''``;`
`if``(data.from_avatar.indexOf(``'https'``) != -1) {`
`icon = data.from_avatar;`
`}``else``{`
`icon = domain + data.from_avatar;`
`}`
`var``notification =``new``Notification(data.from_name, {`
`body: data.content,`
`icon: data.from_avatar`
`});`
`notification.onclick =``function``() {`
`window.focus();`
`notification.close();`
`}`
`break``;`
`default``:`
`console.log(data)`
`}`
`};`
```
其他,因為配置了https 所以要wss然后呢要在nginx中做相關配置 如果是http則無需配置 具體參考文檔:http://doc.workerman.net/faq/secure-websocket-server.html
```
`location``/wss``{`
`proxy_pass http:``//127``.0.0.1:2346;`
`proxy_http_version 1.1;`
`proxy_set_header Upgrade $http_upgrade;`
`proxy_set_header Connection``"upgrade"``;`
`}`
```
注意上述代碼關系到的端口均要在云服務器開通 在云上開通。
- thinkphp6執行流程(一)
- php中use關鍵字用法詳解
- Thinkphp6使用騰訊云發送短信步驟
- 路由配置
- Thinkphp6,static靜態資源訪問路徑問題
- ThinkPHP6.0+ 使用Redis 原始用法
- smarty在thinkphp6.0中的最佳實踐
- Thinkphp6.0 搜索器使用方法
- 從已有安裝包(vendor)恢復 composer.json
- tp6with的用法,表間關聯查詢
- thinkphp6.x多對多如何添加中間表限制條件
- thinkphp6 安裝JWT
- 緩存類型
- 請求信息和HTTP頭信息
- 模型事件用法
- 助手函數匯總
- tp6集成Alipay 手機和電腦端支付的方法
- thinkphp6使用jwt
- 6.0session cookie cache
- tp6筆記
- TP6(thinkphp6)隊列與延時隊列
- thinkphp6 command(自定義指令)
- command(自定義指令)
- 本地文件上傳
- 緩存
- 響應
- 公共函數配置
- 七牛云+文件上傳
- thinkphp6:訪問多個redis數據源(thinkphp6.0.5 / php 7.4.9)
- 富文本編輯器wangEditor3
- IP黑名單
- 增刪改查 +文件上傳
- workerman 定時器操作控制器的方法
- 上傳文件到阿里云oss
- 短信或者郵箱驗證碼防刷代碼
- thinkphp6:訪問redis6(thinkphp 6.0.9/php 8.0.14)
- 實現關聯多個id以逗號分開查詢數據
- thinkphp6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 用mpdf生成pdf文件(php 8.1.1 / thinkphp v6.0.10LTS )
- 生成帶logo的二維碼(php 8.1.1 / thinkphp v6.0.10LTS )
- mysql數據庫使用事務(php 8.1.1 / thinkphp v6.0.10LTS)
- 一,創建過濾IP的中間件
- 源碼解析請求流程
- 驗證碼生成
- 權限管理
- 自定義異常類
- 事件監聽event-listene
- 安裝與使用think-addons
- 事件與多應用
- Workerman 基本使用
- 查詢用戶列表按拼音字母排序
- 擴展包合集
- 查詢用戶數據,但是可以通過輸入用戶昵稱來搜索用戶同時還要統計用戶的文章和粉絲數
- 根據圖片的minetype類型獲取文件真實拓展名思路
- 到處excel
- 用imagemagick庫生成縮略圖
- 生成zip壓縮包并下載
- API 多版本控制
- 用redis+lua做限流(php 8.1.1 / thinkphp v6.0.10LTS )
- 【thinkphp6源碼分析三】 APP類之父, 容器Container類
- thinkphp6表單重復提交解決辦法
- 小程序授權
- 最簡單的thinkphp6導出Excel
- 根據訪問設備不同訪問不同模塊
- 服務系統
- 前置/后置中間件
- 給接口api做簽名驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- 6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 使用前后端分離的驗證碼(thinkphp 6.0.9/php 8.0.14/vue 3.2.26)
- 前后端分離:用jwt+middleware做用戶登錄驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- vue前后端分離多圖上傳
- thinkphp 分組、頁面跳轉與ajax
- thinkphp6 常用方法文檔
- 手冊里沒有的一些用法
- Swagger 3 API 注釋
- PHP 秒級定時任務
- thinkphp6集成gatewayWorker(workerman)實現實時監聽
- thinkphp6按月新增數據表
- 使用redis 實現消息隊列
- api接口 統一結果返回處理類
- 使用swoole+thinkphp6.0+redis 結合開發的登錄模塊
- 給接口api做簽名驗證
- ThinkPHP6.0 + UniApp 實現小程序的 微信登錄
- ThinkPHP6.0 + Vue + ElementUI + axios 的環境安裝到實現 CURD 操作!
- 異常$e
- 參數請求驗證自定義和異常錯誤自定義