Fastadmin(thinkphp5)安装集成workerman(gatewayworker)Linux环境
一、安装
直接上命令
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\admin\library\Auth; use app\workerman\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 [d]|stop|restart|status") ->addArgument('type', Argument::OPTIONAL, "d -d") ->setDescription('websocket 会话服务'); } protected function execute(Input $input, Output $output) { global $argv; $action = trim($input->getArgument('action')); $type = trim($input->getArgument('type')) ? '-d' : ''; $argv[0] = 'notice'; $argv[1] = $action; $argv[2] = $type ? '-d' : ''; $this->start(); } private function start() { // 检查扩展 if (!extension_loaded('pcntl')) { exit("Please install pcntl extension. See http://doc.workerman.net/appendices/install-extension.html\n"); } if (!extension_loaded('posix')) { exit("Please install posix extension. See http://doc.workerman.net/appendices/install-extension.html\n"); } // 标记是全局启动 define('GLOBAL_START', 1); $this->startBusinessWorker(); $this->startGateWay(); $this->startRegister(); Worker::runAll(); } private function startBusinessWorker() { $service_register_port = config('site.service_register_port'); $worker = new BusinessWorker(); $worker->name = 'BusinessWorker'; $worker->count = 4; $worker->registerAddress = '127.0.0.1:'.$service_register_port; $worker->eventHandler = 'app\workerman\WorkerEvents'; } private function startGateWay() { $context = []; $ssl_open = false; $wss_switch = config('site.wss_agreement_open'); $ssl_certificate = config('site.ssl_certificate'); $ssl_certificate_key = config('site.ssl_certificate_key'); if ($wss_switch && $ssl_certificate && $ssl_certificate_key) { $context ['ssl'] = [ // 使用绝对路径 'local_cert' => $ssl_certificate, // 也可以是crt文件 'local_pk' => $ssl_certificate_key, 'verify_peer' => false, //'allow_self_signed' => true, //如果是自签名证书开启此选项 ]; $ssl_open = true; } $websocket_port = config('site.websocket_port'); // gateway 进程 $gateway = new Gateway("websocket://0.0.0.0:".$websocket_port, $context); if ($ssl_open) { // 开始SSL $gateway->transport = 'ssl'; } // 设置名称,方便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个端口作为内部通讯端口 $internal_comm_port = config('site.internal_comm_port'); $gateway->startPort = $internal_comm_port; // 心跳间隔 $gateway->pingInterval = 30; // 时间内,客户端未发送任何数据,断开客户端连接 $gateway->pingNotResponseLimit = 1; // 心跳数据 $gateway->pingData = '{"type":"ping"}'; // 服务注册地址 $service_register_port = config('site.service_register_port'); $gateway->registerAddress = '127.0.0.1:'.$service_register_port; /* // 当客户端连接上来时,设置连接的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() { $service_register_port = config('site.service_register_port'); new Register('text://0.0.0.0:'.$service_register_port); } }
创建新文件路径地址:application/workerman/WorkerEvents.php
文件内容:
<?php namespace app\workerman; 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