recylerobots.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GatewayWorker\Lib\Gateway;
  5. use Illuminate\Support\Facades\Redis;
  6. class recylerobots extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'game:recylerobots';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '机器人资源回收';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. //注册地址
  37. Gateway::$registerAddress = '127.0.0.1:1238';
  38. while(1){
  39. //获取所有机器人
  40. $robots = Redis::lrange('online_robot_list', 0, -1);
  41. if($robots){
  42. foreach ($robots as $robot) {
  43. $message['type'] = 'checkrobot';
  44. //检测是否在线
  45. if(Gateway::isOnline($robot)){
  46. Gateway::sendToClient($robot, json_encode($message));
  47. }else{
  48. Redis::lrem('online_robot_list', 0, $robot);
  49. Redis::srem('robot_list', $robot);
  50. }
  51. }
  52. }
  53. sleep(10);
  54. }
  55. }
  56. }