You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.3 KiB

4 years ago
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GatewayWorker\BusinessWorker;
  5. use GatewayWorker\Gateway;
  6. use GatewayWorker\Register;
  7. use Workerman\Worker;
  8. class GatewayWorkerServer extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'gateway-worker {action} {--d}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Start a GatewayWorker server.';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. global $argv;
  39. $action = $this->argument('action');
  40. if (!in_array($action, ['start', 'stop', 'restart', 'reload', 'status'])) {
  41. exit('Arguments Error');
  42. }
  43. $argv[0] = 'artisan gateway-worker';
  44. $argv[1] = $action;
  45. $argv[2] = $this->option('d') ? '-d' : ''; //必须是一个-,上面定义命令两个--,后台启动用两个--
  46. $this->start();
  47. }
  48. private function start()
  49. {
  50. $this->startGateWay();
  51. $this->startBusinessWorker();
  52. $this->startRegister();
  53. Worker::runAll();
  54. }
  55. private function startBusinessWorker()
  56. {
  57. $worker = new BusinessWorker();
  58. $worker->name = 'BusinessWorker';
  59. $worker->count = 1;
  60. $worker->registerAddress = '127.0.0.1:1236';
  61. $worker->eventHandler = \App\GatewayWorker\Events::class; //设置使用哪个类来处理业务,业务类至少要实现onMessage静态方法,onConnect和onClose静态方法可以不用实现
  62. }
  63. private function startGateWay()
  64. {
  65. $gateway = new Gateway("websocket://0.0.0.0:2346");
  66. $gateway->name = 'Gateway'; //设置BusinessWorker进程的名称
  67. $gateway->count = 1; //设置BusinessWorker进程的数量
  68. $gateway->lanIp = '127.0.0.1'; #内网ip,多服务器分布式部署的时候需要填写真实的内网ip
  69. $gateway->startPort = 2300; //监听本机端口的起始端口
  70. $gateway->pingInterval = 30; //心跳间隔时间(秒)
  71. $gateway->pingNotResponseLimit = 0; //心跳检测的时间间隔数
  72. $gateway->pingData = '{"type":"ping"}'; //心跳消息
  73. $gateway->registerAddress = '127.0.0.1:1236'; //注册服务地址
  74. }
  75. private function startRegister()
  76. {
  77. new Register('text://0.0.0.0:1236');
  78. }
  79. private function init()
  80. {
  81. }
  82. //php artisan gateway-worker start --d之后,打开浏览器F12将内容复制到console里return就行
  83. /* ws = new WebSocket("ws://127.0.0.1:2346?token=123456");
  84. ws.onopen = function() {
  85. ws.send('{"name":"one","user_id":"111"}');
  86. ws.send('{"name":"two","user_id":"222"}');
  87. //定义一个定时器,每10秒钟发送一个包,包的内容随意,一般发送心跳包的间隔在60秒以内
  88. setInterval(function () {
  89. ws.send('{"type":"heart","msg":"Heartbeat reply"}');
  90. }, 10000);
  91. };
  92. ws.onmessage = function(e) {
  93. console.log("收到服务端的消息:" + e.data);
  94. };
  95. ws.onclose = function(e) {
  96. console.log("服务已断开" );
  97. }; */
  98. }