windows环境,workerman能看到打印日志,gateway就是看不到,真的不知道为什么
这是控制台:
这是代码:
GatewayWorker.php
<?php namespace app\common\command; 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\facade\Config; use Workerman\Worker; /** * GatewayWorker 命令行 */ class GatewayWorker extends Command { protected function configure() { $this->setName('gateway') ->addArgument('service', Argument::OPTIONAL, 'workerman service: gateway|register|businessworker', null) ->addArgument('action', Argument::OPTIONAL, "start|stop|restart|reload|status|connections", 'start') ->addOption('host', 'H', Option::VALUE_OPTIONAL, 'the host of workerman server', null) ->addOption('port', 'P', Option::VALUE_OPTIONAL, 'the port of workerman server', null) ->addOption('daemon', 'd', Option::VALUE_OPTIONAL, 'Run the workerman server in daemon mode.') ->setDescription('GatewayWorker Server for ThinkPHP'); } public function execute(Input $input, Output $output) { $service = $input->getArgument('service'); $action = $input->getArgument('action'); if (!in_array($action, ['start', 'stop', 'reload', 'restart', 'status', 'connections'])) { $output->writeln("Invalid argument action:{$action}, Expected start|stop|restart|reload|status|connections ."); exit(1); } global $argv; array_shift($argv); array_shift($argv); array_unshift($argv, 'think', $action); if ('start' == $action) { $output->writeln('Starting GatewayWorker server...'); } $option = Config::get('gateway_worker'); if ($input->hasOption('host')) { $host = $input->getOption('host'); } else { $host = !empty($option['host']) ? $option['host'] : '0.0.0.0'; } if ($input->hasOption('port')) { $port = $input->getOption('port'); } else { $port = !empty($option['port']) ? $option['port'] : '2347'; } $registerAddress = !empty($option['registerAddress']) ? $option['registerAddress'] : '127.0.0.1:1236'; $this->start($host, (int) $port, $option, $registerAddress, $service, $output); } /** * 启动 * @access public * @param string $host 监听地址 * @param integer $port 监听端口 * @param array $option 参数 * @param string $registerAddress 注册地址 * @param string $service 服务名称 * @param Output $output 输出 * @return void */ public function start(string $host, int $port, array $option, string $registerAddress, string $service, Output $output) { switch ($service) { case 'register': $this->register($registerAddress); break; case 'businessworker': $this->businessWorker($registerAddress, $option['businessWorker'] ?? []); break; case 'gateway': $this->gateway($registerAddress, $host, $port, $option); break; default: $output->writeln("<error>Invalid argument action:{$service}, Expected gateway|register|businessworker .</error>"); exit(1); break; } Worker::runAll(); } /** * 启动register * @access public * @param string $registerAddress * @return void */ public function register(string $registerAddress) { // 初始化register new Register('text://' . $registerAddress); } /** * 启动businessWorker * @access public * @param string $registerAddress registerAddress * @param array $option 参数 * @return void */ public function businessWorker(string $registerAddress, array $option = []) { // 初始化 bussinessWorker 进程 $worker = new BusinessWorker(); $this->option($worker, $option); $worker->registerAddress = $registerAddress; } /** * 启动gateway * @access public * @param string $registerAddress registerAddress * @param string $host 服务地址 * @param integer $port 监听端口 * @param array $option 参数 * @return void */ public function gateway(string $registerAddress, string $host, int $port, array $option = []) { // 初始化 gateway 进程 if (!empty($option['socket'])) { $socket = $option['socket']; unset($option['socket']); } else { $protocol = !empty($option['protocol']) ? $option['protocol'] : 'websocket'; $socket = $protocol . '://' . $host . ':' . $port; unset($option['host'], $option['port'], $option['protocol']); } $gateway = new Gateway($socket, $option['context'] ?? []); // 以下设置参数都可以在配置文件中重新定义覆盖 $gateway->name = 'Gateway'; $gateway->count = 4; $gateway->lanIp = '127.0.0.1'; $gateway->startPort = 2000; $gateway->pingInterval = 30; $gateway->pingNotResponseLimit = 0; $gateway->pingData = '{"type":"ping"}'; $gateway->registerAddress = $registerAddress; // 全局静态属性设置 foreach ($option as $name => $val) { if (in_array($name, ['stdoutFile', 'daemonize', 'pidFile', 'logFile'])) { Worker::${$name} = $val; unset($option[$name]); } } $this->option($gateway, $option); } /** * 设置参数 * @access protected * @param Worker $worker Worker对象 * @param array $option 参数 * @return void */ protected function option(Worker $worker, array $option = []) { // 设置参数 if (!empty($option)) { foreach ($option as $key => $val) { $worker->$key = $val; } } } }
Event.php
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <liu21st@gmail.com> // +---------------------------------------------------------------------- namespace app\common\business; use GatewayWorker\Lib\Gateway; use think\worker\Application; use Workerman\Worker; /** * Worker 命令行服务类 */ class Events { /** * onWorkerStart 事件回调 * 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次 * * @access public * @param \Workerman\Worker $businessWorker * @return void */ public static function onWorkerStart(Worker $businessWorker) { $app = new Application; $app->initialize(); } /** * onConnect 事件回调 * 当客户端连接上gateway进程时(TCP三次握手完毕时)触发 * * @access public * @param int $client_id * @return void */ public static function onConnect($client_id) { Gateway::sendToCurrentClient("Your client_id is a $client_id"); } /** * onWebSocketConnect 事件回调 * 当客户端连接上gateway完成websocket握手时触发 * * @param integer $client_id 断开连接的客户端client_id * @param mixed $data * @return void */ public static function onWebSocketConnect($client_id, $data) { var_export($data); } /** * onMessage 事件回调 * 当客户端发来数据(Gateway进程收到数据)后触发 * * @access public * @param int $client_id * @param mixed $data * @return void * @throws \Exception */ public static function onMessage($client_id, $data) { var_dump($data); if($data == 'ping') { file_put_contents('gateway.log', $data . "\n", FILE_APPEND); Gateway::sendToClient($client_id, 'pong'); } else { file_put_contents('gateway.log', $data . "\n", FILE_APPEND); Gateway::sendToAll($data); } } /** * onClose 事件回调 当用户断开连接时触发的方法 * * @param integer $client_id 断开连接的客户端client_id * @return void * @throws \Exception */ public static function onClose($client_id) { GateWay::sendToAll("client[$client_id] logout\n"); } /** * onWorkerStop 事件回调 * 当businessWorker进程退出时触发。每个进程生命周期内都只会触发一次。 * * @param \Workerman\Worker $businessWorker * @return void */ public static function onWorkerStop(Worker $businessWorker) { echo "WorkerStop\n"; } }
下面两行写入log文件都有内容,但是上面那行var_dump的打印在控制台就是看不到
建议用纯净版gatewayWorker试下,感觉是输出被tp给隐藏了
var_export用这个打印看看
这是控制台:
这是代码:
GatewayWorker.php
Event.php
下面两行写入log文件都有内容,但是上面那行var_dump的打印在控制台就是看不到
建议用纯净版gatewayWorker试下,感觉是输出被tp给隐藏了
var_export用这个打印看看