ThinkPHP6中使用GatewayWorker

首先是先安装

复制代码
composer require workerman/gateway-worker
composer require workerman/gatewayclient

下载demo

服务器开通TCP端口82821238

Applications\YourApp目录随便放ThinkPHP6的哪个位置,我这里放在了app\gateway\ws目录中

配置composer.json

json 复制代码
"autoload": {
    "psr-4": {
      "app\\": "app",
      "": "app/gateway/ws" # 增加这一行
    },
    "psr-0": {
      "": "extend/"
    }
  },

保存后执行composer dumpautoload更新

Events.phpstart_businessworker.phpstart_gateway.phpstart_register.php文件中的require_once __DIR__ . '/../../vendor/autoload.php';删除

修改start_gateway.php文件,设置ssl

php 复制代码
// 证书最好是申请的证书
$context = array(
    // 更多ssl选项请参考手册 https://php.net/manual/zh/context.ssl.php
    'ssl' => array(
        // 请使用绝对路径
        'local_cert' => '/www/wwwroot/xxx.xxx.com/app/gateway/ws/ssl/server.pem', // 也可以是crt文件
        'local_pk' => '/www/wwwroot/xxx.xxx.com/app/gateway/ws/ssl/server.key',
        'verify_peer' => false,
        // 'allow_self_signed' => true, //如果是自签名证书需要开启此选项
    )
);
// websocket协议(端口任意,只要没有被其它程序占用就行)
$gateway = new Gateway("websocket://0.0.0.0:8282", $context);
// 开启SSL,websocket+SSL 即wss
$gateway->transport = 'ssl';

// gateway 进程,这里使用Text协议,可以用telnet测试
//$gateway = new Gateway("text://0.0.0.0:8282");

使用ThinkPHP6的自定义命令开启服务

复制代码
php think make:command Ws 

Ws.php内容

php 复制代码
<?php
declare (strict_types=1);

namespace app\command;

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;

class Ws extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('ws')
            ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start')
            ->addOption('mode', 'm', Option::VALUE_OPTIONAL, 'Run the workerman server in daemon mode.')
            ->setDescription('workerman');
    }

    protected function execute(Input $input, Output $output)
    {
        $action = $input->getArgument('action');
        $mode = $input->getOption('mode');

        // 重新构造命令行参数,以便兼容workerman的命令
        global $argv;

        $argv = [];

        array_unshift($argv, 'think', $action);
        if ($mode == 'd') {
            $argv[] = '-d';
        } else if ($mode == 'g') {
            $argv[] = '-g';
        }
        
        // 检查扩展
        if (!extension_loaded('pcntl')) {
            exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        }

        if (!extension_loaded('posix')) {
            exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
        }

        // 标记是全局启动
        define('GLOBAL_START', 1);

        foreach (glob(app()->getBasePath() . '/gateway/*/start*.php') as $start_file) {
            require_once $start_file;
        }

        Worker::runAll();
    }
}

配置自定义指令,在config/console.php中增加

php 复制代码
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
    // 指令定义
    'commands' => [
        'ws' => \app\command\Ws::class # 增加
    ],
];

命令启动服务

复制代码
# debug启动
php think ws start 
# 守护进程启动
php think ws start --mode d
# 停止服务
php think ws stop

我是docker配置的环境,报错了Please install pcntl extension.

因为pcntl是php自带的扩展,所以只要执行命令安装一下就可以

复制代码
docker-php-ext-install pcntl  

如果没有执行composer dumpautoload运行时可能会报错:Waring: Events::onMessage is not callable

相关推荐
BingoGo1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack1 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack2 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo3 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack4 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
QQ5110082855 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe5 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5