// +---------------------------------------------------------------------- // app/command/StartWorkerman.php namespace ywxapp\command; use think\console\Command; use think\console\Input; use think\console\Output; use think\console\input\Option; use GatewayWorker\Gateway; use GatewayWorker\BusinessWorker; use GatewayWorker\Register; use think\facade\App; use think\facade\Config; /** * GatewayWorker 类 * * @author ywxapp */ class GatewayWorker extends Command { protected function configure() { $this->setName('workerman:gateway') ->addOption('acthon', 'a', Option::VALUE_OPTIONAL, '操作') ->setDescription('Start the Workerman server'); } protected function execute(Input $input, Output $output) { // register 必须是text协议,切记不能将register端口开放给外网 $register = new Register('text://127.0.0.1:1238'); // bussinessWorker 进程 $worker = new BusinessWorker(); $worker->name = 'ChatBusinessWorker'; $worker->count = 4; $worker->registerAddress = '127.0.0.1:1238'; $worker->eventHandler = '\\ywxapp\handler\BusinessHandler'; $context = array( 'ssl' => array( 'local_cert' => __DIR__ . '/fullchain.pem', // 也可以是crt文件 'local_pk' => __DIR__ . '/privkey.pem', 'verify_peer' => false, 'allow_self_signed' => true, //如果是自签名证书需要开启此选项 ) ); $gateway = new Gateway("websocket://0.0.0.0:8282", $context); $gateway->name = 'AppGateway'; $gateway->count = 2; $gateway->lanIp = '127.0.0.1'; $gateway->startPort = 2900; $gateway->registerAddress = '127.0.0.1:1238'; //$gateway->onWorkerStart //$gateway->onWorkerStop //$gateway->onConnect //$gateway->onMessage //$gateway->onClose $gateway->transport = 'ssl'; // 初始化 ThinkPHP 应用 $app = App::getInstance(); $gateway->statusCallback = function ($status) use ($app) { // 在这里可以使用 ThinkPHP 容器 $app->make(\ywxapp\services\AppService::class); // print_r($app->user); // 你可以在这里调用 ThinkPHP 的服务 // $service = $app->make(\app\service\MyService::class); // $service->doSomething(); // 处理工作进程状态变化 // 例如:记录日志,发送通知等 file_put_contents('status.log', json_encode($status) . PHP_EOL, FILE_APPEND); }; // $gateway->pingInterval = 10; // $gateway->pingNotResponseLimit = 1; // $gateway->pingData = '{"type":"ping"}'; Gateway::runAll(); // 输出启动信息 $output->writeln("Workerman server started on http://0.0.0.0:2345"); } }