Przeglądaj źródła

添加回收机制

郑晓宇 6 lat temu
rodzic
commit
d21ebd789d
1 zmienionych plików z 64 dodań i 0 usunięć
  1. 64 0
      app/Console/Commands/recylerobots.php

+ 64 - 0
app/Console/Commands/recylerobots.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use GatewayWorker\Lib\Gateway;
+use Illuminate\Support\Facades\Redis;
+
+class recylerobots extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'game:recylerobots';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '机器人资源回收';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        //注册地址
+        Gateway::$registerAddress = '127.0.0.1:1238';
+        while(1){
+            //获取所有机器人
+            $robots = Redis::lrange('online_robot_list', 0);
+            if($robots){
+                foreach ($robots as $robot) {
+                    $message['type'] = 'checkrobot';
+                    $message['client_id'] = $robot;
+                    //检测是否在线
+                    if(Gateway::isOnline($robot)){
+                        Gateway::sendToClient($robot, json_encode($message));
+                    }else{
+                        Redis::lrem('online_robot_list', 0, $robot);
+                        Redis::srem('robot_list', $robot);
+                    }
+                }
+            }
+            sleep(10);
+        }
+
+    }
+}