当前位置: 首页 > news >正文

ThinkPHP6中使用GatewayWorker

首先是先安装

composer require workerman/gateway-worker
composer require workerman/gatewayclient

下载demo

服务器开通TCP端口82821238

Applications\YourApp目录随便放ThinkPHP6的哪个位置,我这里放在了app\gateway\ws目录中
配置composer.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

// 证书最好是申请的证书
$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
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
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
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

相关文章:

  • AI新纪元:可能的盈利之道
  • QT3作业
  • pikachu靶场-File Inclusion
  • C语言--贪吃蛇
  • [LWC] Components Communication
  • 2023 最新 IntelliJ IDEA 2023.3 详细配置步骤演示(图文版)<中文版>
  • 数字签名在游戏里的应用
  • 如何实现多账户管理?海外代理IP推荐
  • 【2024.02.22】定时执行专家 V7.0 发布 - TimingExecutor V7.0 Release - 龙年春节重大更新版本
  • 多维时序 | Matlab实现基于VMD-DBO-GRU、VMD-GRU、GRU的多变量时间序列预测
  • 【PostgreSQL】PostgreSQL详细介绍
  • 怎么才能快速准确地全面地了解一个陌生的行业?
  • matlab经验模式分解的R波检测算法
  • 图像压缩感知的MATLAB实现(OMP)
  • 外包干了3个月,技术倒退1年。。。
  • Google 是如何开发 Web 框架的
  • Akka系列(七):Actor持久化之Akka persistence
  • Android组件 - 收藏集 - 掘金
  • bearychat的java client
  • Hibernate【inverse和cascade属性】知识要点
  • JavaScript设计模式系列一:工厂模式
  • Java编程基础24——递归练习
  • Lucene解析 - 基本概念
  • nginx 配置多 域名 + 多 https
  • Python socket服务器端、客户端传送信息
  • Spark RDD学习: aggregate函数
  • 编写符合Python风格的对象
  • 产品三维模型在线预览
  • 你真的知道 == 和 equals 的区别吗?
  • 前端存储 - localStorage
  • 06-01 点餐小程序前台界面搭建
  • Mac 上flink的安装与启动
  • ​ubuntu下安装kvm虚拟机
  • # 数据结构
  • $NOIp2018$劝退记
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (c语言)strcpy函数用法
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (三)centos7案例实战—vmware虚拟机硬盘挂载与卸载
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)h264中avc和flv数据的解析
  • (转)ORM
  • ..thread“main“ com.fasterxml.jackson.databind.JsonMappingException: Jackson version is too old 2.3.1
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .net访问oracle数据库性能问题
  • .NET设计模式(8):适配器模式(Adapter Pattern)
  • /usr/bin/perl:bad interpreter:No such file or directory 的解决办法
  • @JsonSerialize注解的使用
  • []常用AT命令解释()
  • [1] 平面(Plane)图形的生成算法
  • [④ADRV902x]: Digital Filter Configuration(发射端)
  • [C#]winform制作仪表盘好用的表盘控件和使用方法
  • [C++] new和delete
  • [CareerCup][Google Interview] 实现一个具有get_min的Queue