环境:基于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"; }
注意上述代码关系到的端口均要在云服务器开通 在云上开通。