郑晓宇 6 年之前
父節點
當前提交
2a0554c0cf
共有 2 個文件被更改,包括 16 次插入7 次删除
  1. 4 0
      app/Console/Commands/gamematch.php
  2. 12 7
      app/Home/Controllers/GameController.php

+ 4 - 0
app/Console/Commands/gamematch.php

@@ -81,6 +81,10 @@ class gamematch extends Command
                 Gateway::sendToGroup($room['room_id'], json_encode($message));
                 echo "匹配成功: {$data['client_id']} 、 {$room['client_id']}\n";
 
+                //设置session
+                Gateway::setSession($room['user_id'], ['room_id'=>$room['room_id']]);
+                Gateway::setSession($data['user_id'], ['room_id'=>$room['room_id']]);
+
                 //发送题目
                 $question = Question::inRandomOrder()->select('question_id','title')->where("is_released",1)->first();
                 $options = Option::select('option_id','title')->where("question_id",$question->question_id)->get($question->question_id);

+ 12 - 7
app/Home/Controllers/GameController.php

@@ -94,17 +94,24 @@ class GameController extends Controller
             $response['msg'] = '请先登录';
             return response()->json($response);
         }
-        $room_id = $request->input('room_id');
+
+        $message['is_end'] = 0;  //是否结束答题标志
+        $question_id = $request->input('question_id');
+        $option_id = $request->input('option_id');
+        $user_id = $request->session()->get('user_id');
+
+        //检测用户断线
+        Gateway::$registerAddress = '127.0.0.1:1238';
+        //获取当前用户session
+        $gateway_user = Gateway::getSession($user_id);
+        $room_id = $gateway_user['room_id'];
+        
         if(!$room_id){
             $response['code'] = 400;
             $response['msg'] = '缺少房间参数';
             return response()->json($response);
         }
 
-        $message['is_end'] = 0;  //是否结束答题标志
-        $question_id = $request->input('question_id');
-        $option_id = $request->input('option_id');
-        $user_id = $request->session()->get('user_id');
 
         //获取当前房间问题条数
         $questions = Redis::get($room_id . '_questions');
@@ -114,8 +121,6 @@ class GameController extends Controller
         }else{
             $questions_count = 0;
         }
-        //检测用户断线
-        Gateway::$registerAddress = '127.0.0.1:1238';
         //获取当前组存活用户
         $clients = Gateway::getUidListByGroup($room_id);
         if(count($clients) < 2){