一、安装
直接上命令
composer require topthink/think-worker 1.0.1 //因为fastadmin的tp版本是5,所以这里1.0.1
composer update --with-all-dependencies
找到根目录的composer.json修改制定版本
composer require workerman/gateway-worker
至此安装完成。
二、启动
然后在项目中增加相应的文件启动wokerman gateway
找到路径文件 application/command.php
'app\common\command\Workerman', // workerman //增加一行
//文件内容
<?php namespace app\common\command; use app\workerman\controller\WorkerEvents; 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 think\Env; use Workerman\Worker; class Workerman extends Command { protected function configure() { $this->setName('workerman') ->addArgument('action', Argument::OPTIONAL, "action start|stop|restart") ->addArgument('type', Argument::OPTIONAL, "d -d") ->setDescription('workerman chat'); } protected function execute(Input $input, Output $output) { global $argv; $action = trim($input->getArgument('action')); $type = trim($input->getArgument('type')) ? '-d' : ''; $argv[0] = 'chat'; $argv[1] = $action; $argv[2] = $type ? '-d' : ''; // dump($argv);exit; $this->start(); } private function start() { $this->startGateWay(); $this->startBusinessWorker(); $this->startRegister(); Worker::runAll(); } private function startBusinessWorker() { $worker = new BusinessWorker(); $worker->name = 'BusinessWorker'; $worker->count = 1; $worker->registerAddress = '127.0.0.1:1236'; $worker->eventHandler = WorkerEvents::class; } private function startGateWay() { // gateway 进程 $gateway = new Gateway("websocket://0.0.0.0:39001"); // 设置名称,方便status时查看 $gateway->name = 'Gateway'; // 设置进程数,gateway进程数建议与cpu核数相同 $gateway->count = 4; // 分布式部署时请设置成内网ip(非127.0.0.1) $gateway->lanIp = '127.0.0.1'; // 内部通讯起始端口,假如$gateway->count=4,起始端口为4000 // 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口 $gateway->startPort = 2300; // 心跳间隔 $gateway->pingInterval = 60; // 时间内,客户端未发送任何数据,断开客户端连接 $gateway->pingNotResponseLimit = 180; // 心跳数据 $gateway->pingData = '{"type":"ping"}'; // 服务注册地址 $gateway->registerAddress = '127.0.0.1:1238'; /* // 当客户端连接上来时,设置连接的onWebSocketConnect,即在websocket握手时的回调 $gateway->onConnect = function($connection) { $connection->onWebSocketConnect = function($connection , $http_header) { // 可以在这里判断连接来源是否合法,不合法就关掉连接 // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接 if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net') { $connection->close(); } // onWebSocketConnect 里面$_GET $_SERVER是可用的 // var_dump($_GET, $_SERVER); }; }; */ } private function startRegister() { new Register('text://0.0.0.0:1236'); } }
创建新文件路径地址:application/workerman/WorkerEvents.php
文件内容:
<?php namespace app\workerman\controller; use GatewayWorker\Lib\Gateway; class WorkerEvents { /** * 当客户端连接时触发 * 如果业务不需此回调可以删除onConnect * * @param int $client_id 连接id */ public static function onConnect($client_id) { $data['type'] = 'get_client_id'; $data['data'] = ['client_id' => $client_id]; // 向当前client_id发送数据 Gateway::sendToClient($client_id, json_encode($data, JSON_UNESCAPED_UNICODE)); } /** * 当客户端发来消息时触发 * @param int $client_id 连接id * @param mixed $message 具体消息 */ public static function onMessage($client_id, $message) { dump("收到消息"); dump($client_id); dump($message); //不作处理 } /** * 当用户断开连接时触发 * @param int $client_id 连接id */ public static function onClose($client_id) { dump("断开连接".$client_id); } }
启动服务
php think workerman start
标签:function,worker,thinkphp5,gateway,client,Fastadmin,workerman,id From: https://www.cnblogs.com/wt645631686/p/18447825