WorkermanTCPCommand.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Console\Commands;
  3. use GatewayWorker\BusinessWorker;
  4. use GatewayWorker\Gateway;
  5. use GatewayWorker\Register;
  6. use Illuminate\Console\Command;
  7. use Workerman\Worker;
  8. class WorkermanTCPCommand extends Command
  9. {
  10. protected $signature = 'workmantcp {action} {--d}';
  11. protected $description = 'Start a Workerman server.';
  12. public function handle()
  13. {
  14. global $argv;
  15. $action = $this->argument('action');
  16. $argv[0] = 'wktcp';
  17. $argv[1] = $action;
  18. $argv[2] = $this->option('d') ? '-d' : '';
  19. $this->start();
  20. }
  21. private function start()
  22. {
  23. $this->startGateWay();
  24. $this->startBusinessWorker();
  25. $this->startRegister();
  26. // $this->startSendWorker();
  27. Worker::runAll();
  28. }
  29. private function startBusinessWorker()
  30. {
  31. $worker = new BusinessWorker();
  32. //work名称
  33. $worker->name = 'BusinessWorker';
  34. //businessWork进程数
  35. $worker->count = 1;
  36. //服务注册地址
  37. $worker->registerAddress = '127.0.0.1:1237';
  38. //设置\App\Workerman\Events类来处理业务
  39. $worker->eventHandler = \App\Workerman\EventsTCP::class;
  40. }
  41. // private function startSendWorker(){
  42. // $text_worker = new Gateway("text://0.0.0.0:5678");
  43. //
  44. // $text_worker->onMessage = function($connection, $data)
  45. // {
  46. // $connection->send("hello world");
  47. // };
  48. // }
  49. private function startGateWay()
  50. {
  51. //gateway进程
  52. $gateway = new Gateway("tcp://0.0.0.0:2347");
  53. //gateway名称 status方便查看
  54. $gateway->name = 'Gateway';
  55. //gateway进程
  56. $gateway->count = 1;
  57. //本机ip
  58. $gateway->lanIp = '127.0.0.1';
  59. //内部通讯起始端口,如果$gateway->count = 4 起始端口为2300
  60. //则一般会使用 2300,2301 2个端口作为内部通讯端口
  61. $gateway->startPort = 2300;
  62. //心跳间隔
  63. $gateway->pingInterval = 30;
  64. //客户端连续$pingNotResponseLimit次$pingInterval时间内不发送任何数据则断开链接,并触发onClose。
  65. //我们这里使用的是服务端主动发送心跳所以设置为0
  66. $gateway->pingNotResponseLimit = 0;
  67. //心跳数据
  68. $gateway->pingData = '{"type":"@heart@"}';
  69. //服务注册地址
  70. $gateway->registerAddress = '127.0.0.1:1237';
  71. }
  72. private function startRegister()
  73. {
  74. new Register('text://0.0.0.0:1237');
  75. }
  76. }