/** * 当客户端断开连接时 * @param integer $client_id 客户端id */ public static function onClose($client_id) { // debug echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:{$client_id} onClose:''\n"; // 从房间的客户端列表中删除 if (isset($_SESSION['room_id'])) { $room_id = $_SESSION['room_id']; $new_message = array('type' => 'logout', 'from_client_id' => $client_id, 'from_client_name' => $_SESSION['client_name'], 'time' => date('Y-m-d H:i:s')); Gateway::sendToGroup($room_id, json_encode($new_message)); } }
private function login($client_id, $message_data) { // 判断是否有房间号 if (!isset($message_data['room_id'])) { throw new \Exception("\$message_data['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']} \$message:{$message}"); } //获取用户客户端登陆的身份 $uid = $message_data['uid']; $room_id = $message_data['room_id']; //获取用户提交上来的 房间号 $_SESSION['room_id'] = $room_id; //将房间号存入 session $memcache = new Memcache(); //从缓存服务器中取出当前登陆用户的信息 $userInfo = $memcache->getUserInfo($uid); //从缓存服务器 $_SESSION['userInfo'] = $userInfo; //把缓存数据 存进session 绑定当前连接用户 $_SESSION['client_name'] = $userInfo['nick_name'] ? $userInfo['nick_name'] : $userInfo['username']; // 获取房间内所有用户列表 $clients_list = Gateway::getClientInfoByGroup($room_id); foreach ($clients_list as $tmp_client_id => $item) { $clients_list[$tmp_client_id] = ['uid' => $item['userInfo']['id'], 'client_name' => $item['userInfo']['nick_name'] ? $item['userInfo']['nick_name'] : $item['userInfo']['username'], 'head_portrait' => $item['userInfo']['head_portrait']]; } $clients_list[$client_id] = ['uid' => $userInfo['id'], 'client_name' => $userInfo['nick_name'] ? $userInfo['nick_name'] : $userInfo['username'], 'head_portrait' => $userInfo['head_portrait']]; $id = Gateway::getClientIdByUid($uid); //获取id 绑定下的 client_id 1对多关系 if (empty($id)) { //新用户登陆 // 转播给当前房间的所有客户端,xx进入聊天室 message {type:login, client_id:xx, name:xx} $new_message = array('type' => $message_data['type'], 'client_id' => $client_id, 'uid' => $uid, 'head_portrait' => $userInfo['head_portrait'], 'client_name' => $_SESSION['client_name'], 'time' => date('Y-m-d H:i:s')); Gateway::sendToGroup($room_id, json_encode($new_message)); // 发送所有人 Gateway::joinGroup($client_id, $room_id); //将当前连接人 添加到房间内 Gateway::bindUid($client_id, $uid); //将当前连接人 绑定用户id 1对多关系 } else { //页面多开 Gateway::bindUid($client_id, $uid); //将当前连接人 绑定用户id 1对多关系 } // 给当前用户发送用户列表 $new_message['client_list'] = $clients_list; Gateway::sendToCurrentClient(json_encode($new_message)); return; }
/** * 当客户端断开连接时 * @param integer $client_id 客户端id */ public static function onClose($client_id) { // debug // echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n"; // 从房间的客户端列表中删除 $adminUid = 1; if (isset($_SESSION['room_id'])) { $room_id = $_SESSION['room_id']; $uid = $_SESSION['uid']; $logined = Gateway::getClientIdByUid($uid); //uid 还在线 if (empty($logined)) { $new_message = array('type' => 'logout', 'from_uid_id' => $uid, 'from_client_name' => $_SESSION['client_name'], 'time' => date('Y-m-d H:i:s')); Gateway::sendToGroup($room_id, json_encode($new_message)); //发给管理员,要看所有的在线人数 $new_logout = ['type' => 'loggoutTwo']; Gateway::sendToUid($adminUid, json_encode($new_logout)); } } }
/** * 当客户端断开连接时 * @param integer $client_id 客户端id */ public static function onClose($client_id) { // debug echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:{$client_id} onClose:''\n"; // 从房间的客户端列表中删除 if (isset($_SESSION['room_id'])) { $room_id = $_SESSION['room_id']; $new_message = array('type' => 'game', 'fun' => 'rm_opp()'); Gateway::sendToGroup($room_id, json_encode($new_message)); } }