Example #1
0
 /**
  * 有消息时
  * @param int $client_id
  * @param string $message
  */
 public static function onMessage($client_id, $message)
 {
     // 如果是websocket握手
     if (self::checkHandshake($message)) {
         // debug
         echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']}  client_id:{$client_id} onMessage:" . $message . "\n";
         return;
     }
     // 判断是不是websocket的关闭连接的包
     if (WebSocket::isClosePacket($message)) {
         return Gateway::kickClient($client_id);
     }
     // 解码websocket,得到原始数据
     $message = WebSocket::decode($message);
     // debug
     //echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']}  client_id:$client_id session:".json_encode($_SESSION)." onMessage:".$message."\n";
     // 客户端传递的是json数据
     $data = json_decode($message, true);
     print_r($data);
     // 判断数据是否正确
     if (empty($data['class']) || empty($data['method']) || !isset($data['param_array'])) {
         // 发送数据给客户端,请求包错误
         $result = json_encode(array('code' => 400, 'msg' => 'bad request', 'data' => null));
         return Gateway::sendToCurrentClient(WebSocket::encode($result));
     }
     // 获得要调用的类、方法、及参数
     $class = $data['class'];
     $method = $data['method'];
     $param_array = $data['param_array'];
     // 判断类对应文件是否载入
     if (!class_exists($class)) {
         $include_file = ROOT_DIR . "/Services/{$class}.php";
         if (is_file($include_file)) {
             require_once $include_file;
         }
         if (!class_exists($class)) {
             $code = 404;
             $msg = "class {$class} not found";
             // 发送数据给客户端 类不存在
             $result = json_encode(array('code' => $code, 'msg' => $msg, 'data' => null));
             return Gateway::sendToCurrentClient(WebSocket::encode($result));
         }
     }
     // 调用类的方法
     try {
         $ret = call_user_func_array(array($class, $method), $param_array);
         // 发送数据给客户端,调用成功,data下标对应的元素即为调用结果
         $result = json_encode(array('code' => 0, 'msg' => 'ok', 'data' => $ret));
         return Gateway::sendToCurrentClient(WebSocket::encode($result));
     } catch (Exception $e) {
         // 发送数据给客户端,发生异常,调用失败
         $code = $e->getCode() ? $e->getCode() : 500;
         $result = json_encode(array('code' => $code, 'msg' => $e->getMessage(), 'data' => $e));
         return Gateway::sendToCurrentClient(WebSocket::encode($result));
     }
 }
Example #2
0
 /**
  * 有消息时
  * @param int $uid
  * @param string $message
  */
 public static function onMessage($uid, $message)
 {
     // $message len < 7 可能是ping包,断开连接的包等暂时忽略
     if (strlen($message) < 7) {
         return;
     }
     $message = WebSocket::decode($message);
     // 广播路线
     Gateway::sendToAll(WebSocket::encode(pack('CVVVC', 2, 1, 1, $uid, 0) . substr($message, 1)));
 }
Example #3
0
 /**
  * 有消息时
  * @param int $uid
  * @param string $message
  */
 public static function onMessage($uid, $message)
 {
     if (WebSocket::isClosePacket($message)) {
         Gateway::kickUid($uid, '');
         self::onClose($uid);
         return;
     }
     $message = WebSocket::decode($message);
     // debug
     echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} socketid:{$_SERVER['GATEWAY_SOCKET_ID']} uid:{$uid} onMessage:" . $message . "\n";
     $message_data = json_decode($message, true);
     if (!$message_data) {
         return;
     }
     switch ($message_data['type']) {
         // 用户登录 message格式: {type:login, name:xx} ,添加到用户,广播给所有用户xx进入聊天室
         case 'login':
             // 存储当前用户到用户列表
             self::addUserToList($uid, htmlspecialchars($message_data['name']));
             // 获取用户列表
             $user_list = self::getUserList();
             // 整理用户列表以便显示
             $all_users = array();
             if ($user_list) {
                 foreach ($user_list as $tmp_uid => $name) {
                     $all_users[] = array('uid' => $tmp_uid, 'name' => $name);
                 }
             }
             // 发送给当前用户 内容是用户列表 message: {type:user_list, user_list:xxxx}
             Gateway::sendToUid($uid, WebSocket::encode(json_encode(array('type' => 'user_list', 'user_list' => $all_users))));
             // 转播给所有用户,xx进入聊天室 message {type:login, uid:xx, name:xx}
             Gateway::sendToAll(WebSocket::encode(json_encode(array('type' => 'login', 'uid' => $uid, 'name' => htmlspecialchars($message_data['name']), 'time' => date('Y-m-d H:i:s')))));
             return;
             // 用户发言 message: {type:say, to_uid:xx, content:xx}
         // 用户发言 message: {type:say, to_uid:xx, content:xx}
         case 'say':
             // 私聊
             if ($message_data['to_uid'] != 'all') {
                 $new_message = array('type' => 'say', 'from_uid' => $uid, 'to_uid' => $message_data['to_uid'], 'content' => nl2br(htmlspecialchars($message_data['content'])), 'time' => date('Y-m-d :i:s'));
                 echo 'here';
                 return Gateway::sendToUid($message_data['to_uid'], WebSocket::encode(json_encode($new_message)));
             }
             // 向大家说
             $new_message = array('type' => 'say', 'from_uid' => $uid, 'to_uid' => 'all', 'content' => nl2br(htmlspecialchars($message_data['content'])), 'time' => date('Y-m-d :i:s'));
             return Gateway::sendToAll(WebSocket::encode(json_encode($new_message)));
     }
 }
Example #4
0
 /**
  * 当用户断开连接时
  * @param integer $client_id 用户id
  */
 public static function onClose($client_id)
 {
     // 广播 xxx 退出了
     GateWay::sendToAll(WebSocket::encode(json_encode(array('type' => 'closed', 'id' => $client_id))));
 }