123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Console\Commands;
- use GatewayWorker\BusinessWorker;
- use GatewayWorker\Gateway;
- use GatewayWorker\Register;
- use Illuminate\Console\Command;
- use Workerman\Worker;
- class WorkermanTCPCommand extends Command
- {
- protected $signature = 'workmantcp {action} {--d}';
- protected $description = 'Start a Workerman server.';
- public function handle()
- {
- global $argv;
- $action = $this->argument('action');
- $argv[0] = 'wktcp';
- $argv[1] = $action;
- $argv[2] = $this->option('d') ? '-d' : '';
- $this->start();
- }
- private function start()
- {
- $this->startGateWay();
- $this->startBusinessWorker();
- $this->startRegister();
- // $this->startSendWorker();
- Worker::runAll();
- }
- private function startBusinessWorker()
- {
- $worker = new BusinessWorker();
- //work名称
- $worker->name = 'BusinessWorker';
- //businessWork进程数
- $worker->count = 1;
- //服务注册地址
- $worker->registerAddress = '127.0.0.1:1237';
- //设置\App\Workerman\Events类来处理业务
- $worker->eventHandler = \App\Workerman\EventsTCP::class;
- }
- // private function startSendWorker(){
- // $text_worker = new Gateway("text://0.0.0.0:5678");
- //
- // $text_worker->onMessage = function($connection, $data)
- // {
- // $connection->send("hello world");
- // };
- // }
- private function startGateWay()
- {
- //gateway进程
- $gateway = new Gateway("tcp://0.0.0.0:2347");
- //gateway名称 status方便查看
- $gateway->name = 'Gateway';
- //gateway进程
- $gateway->count = 1;
- //本机ip
- $gateway->lanIp = '127.0.0.1';
- //内部通讯起始端口,如果$gateway->count = 4 起始端口为2300
- //则一般会使用 2300,2301 2个端口作为内部通讯端口
- $gateway->startPort = 2300;
- //心跳间隔
- $gateway->pingInterval = 30;
- //客户端连续$pingNotResponseLimit次$pingInterval时间内不发送任何数据则断开链接,并触发onClose。
- //我们这里使用的是服务端主动发送心跳所以设置为0
- $gateway->pingNotResponseLimit = 0;
- //心跳数据
- $gateway->pingData = '{"type":"@heart@"}';
- //服务注册地址
- $gateway->registerAddress = '127.0.0.1:1237';
- }
- private function startRegister()
- {
- new Register('text://0.0.0.0:1237');
- }
- }
|