argument('username'); if(!$username){ $users = array("晚桐","书蝶","北俞","时宛","花染"); $rand_user = rand(0,4); $username = $users[$rand_user]; } $worker = new Worker(); $worker->onWorkerStart = function($worker) use($username){ //定义请求客户端 $client = new Client([ 'cookies' => true, 'verify' => false, ]); $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Login',[ 'form_params' => [ 'username' => $username, ] ]); $response = $client->request('GET', 'http://183.234.61.252:8090/Home/User/Info'); $robot = $response->getBody()->getContents(); $con = new AsyncTcpConnection('ws://183.234.61.252:8282'); $con->onConnect = function($con) use($client) { }; $con->onMessage = function($con, $data) use($client){ $json_data = json_decode($data); //初始化 if($json_data->type == 'init'){ $response = $client->request('POST', 'http://183.234.61.252:8090/Home/User/Bind',[ 'form_params' => [ 'client_id' => $json_data->client_id, ] ]); echo "成功绑定socket\n"; //测试使用直接加入匹配 // $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[ // 'form_params' => [ // 'client_id' => $json_data->client_id, // ] // ]); //记录当前在线机器人 Redis::lpush('robot_list', $json_data->client_id); } //唤起匹配 if($json_data->type == 'gotomatch'){ var_dump($json_data); $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Join',[ 'form_params' => [ 'client_id' => $json_data->client_id, ] ]); echo "成功加入匹配\n"; } //获得题目 if($json_data->type == 'question'){ $question_id = $json_data->info->question->question_id; $options = $json_data->info->options; $answer = rand(0, count($json_data->info->options) - 1); //随机几秒回答问题 $second = rand(0,6); sleep($second); $response = $client->request('POST', 'http://183.234.61.252:8090/Home/Game/Answer',[ 'form_params' => [ 'question_id' => $question_id, 'option_id' => $options[$answer], ] ]); } //获得题目 if($json_data->type == 'answer'){ if($json_data->is_end == 1){ echo "成功重回队列\n"; //重新加入机器人队列 Redis::lpush('robot_list', $json_data->client_id); } } }; $con->connect(); }; Worker::runAll(); } }