首先是先安裝
composer require workerman/gateway-worker
composer require workerman/gatewayclient
下載demo
服務(wù)器開通TCP端口8282
、1238
將Applications\YourApp
目錄隨便放ThinkPHP6的哪個(gè)位置,我這里放在了app\gateway\ws
目錄中
配置composer.json
"autoload": {
"psr-4": {
"app\\": "app",
"": "app/gateway/ws" # 增加這一行
},
"psr-0": {
"": "extend/"
}
},
保存后執(zhí)行composer dumpautoload
更新
將Events.php
、start_businessworker.php
、start_gateway.php
和 start_register.php
文件中的require_once __DIR__ . '/../../vendor/autoload.php';
刪除
修改start_gateway.php
文件,設(shè)置ssl
// 證書最好是申請(qǐng)的證書
$context = array(
// 更多ssl選項(xiàng)請(qǐng)參考手冊(cè) https://php.net/manual/zh/context.ssl.php
'ssl' => array(
// 請(qǐng)使用絕對(duì)路徑
'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, //如果是自簽名證書需要開啟此選項(xiàng)
)
);
// websocket協(xié)議(端口任意,只要沒有被其它程序占用就行)
$gateway = new Gateway("websocket://0.0.0.0:8282", $context);
// 開啟SSL,websocket+SSL 即wss
$gateway->transport = 'ssl';
// gateway 進(jìn)程,這里使用Text協(xié)議,可以用telnet測試
//$gateway = new Gateway("text://0.0.0.0:8282");
使用ThinkPHP6的自定義命令開啟服務(wù)
php think make:command Ws
Ws.php內(nèi)容
<?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');
// 重新構(gòu)造命令行參數(shù),以便兼容workerman的命令
global $argv;
$argv = [];
array_unshift($argv, 'think', $action);
if ($mode == 'd') {
$argv[] = '-d';
} else if ($mode == 'g') {
$argv[] = '-g';
}
// 檢查擴(kuò)展
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");
}
// 標(biāo)記是全局啟動(dòng)
define('GLOBAL_START', 1);
foreach (glob(app()->getBasePath() . '/gateway/*/start*.php') as $start_file) {
require_once $start_file;
}
Worker::runAll();
}
}
配置自定義指令,在config/console.php
中增加
<?php
// +----------------------------------------------------------------------
// | 控制臺(tái)配置
// +----------------------------------------------------------------------
return [
// 指令定義
'commands' => [
'ws' => \app\command\Ws::class # 增加
],
];
命令啟動(dòng)服務(wù)
# debug啟動(dòng)
php think ws start
# 守護(hù)進(jìn)程啟動(dòng)
php think ws start --mode d
# 停止服務(wù)
php think ws stop
我是docker
配置的環(huán)境,報(bào)錯(cuò)了Please install pcntl extension.
因?yàn)?code>pcntl是php自帶的擴(kuò)展,所以只要執(zhí)行命令安裝一下就可以文章來源:http://www.zghlxwxcb.cn/news/detail-836073.html
docker-php-ext-install pcntl
如果沒有執(zhí)行composer dumpautoload
運(yùn)行時(shí)可能會(huì)報(bào)錯(cuò):Waring: Events::onMessage is not callable
文章來源地址http://www.zghlxwxcb.cn/news/detail-836073.html
到了這里,關(guān)于ThinkPHP6中使用GatewayWorker的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!