/** * 有消息时触发该方法 * @param int $client_id 发消息的client_id * @param string $message 消息 * @return void */ public static function onMessage($client_id, $message) { $db = \Lib\Db::instance('tc504'); $nowtime = idate("U"); $ok = "right"; $error = "error"; $sqlok = "sql right!"; $sqlerror = "sql error!"; if ($message[0] == '{') { $message = JsonProtocol::decode($message); $sqldidian = $message['ID']; $sqlshidu = $message['humidity']; $sqlwendu = $message['temperature']; $insert_id = $db->insert('wenshidu')->cols(array('didian' => $sqldidian, 'shidu' => $sqlshidu, 'wendu' => $sqlwendu, 'time' => $nowtime))->query(); GateWay::sendToClient($client_id, TextProtocol::encode($insert_id)); } else { $message = TextProtocol::decode($message); $commend = trim($message); $ret = $db->select('ui_cardid')->from('tbl_user')->where("ui_cardid = '{$commend}' ")->single(); if ($commend === $ret) { $insert_id = $db->insert('tbl_log')->cols(array('log_card' => $commend, 'log_time' => $nowtime))->query(); if ($insert_id === 1) { GateWay::sendToClient($client_id, TextProtocol::encode($sqlok)); } else { GateWay::sendToClient($client_id, TextProtocol::encode($sqlerror)); } } else { GateWay::sendToClient($client_id, TextProtocol::encode($error)); } } }
/** * 有消息时触发该方法 * @param int $uid 发消息的uid * @param string $message 消息 * @return void */ public static function onMessage($uid, $message) { $message_data = TextProtocol::decode($message); // 判断是否是私聊,私聊数据格式 uid:xxxxx $explode_array = explode(':', $message, 2); if (count($explode_array) > 1) { $to_uid = (int) $explode_array[0]; GateWay::sendToUid($uid, TextProtocol::encode($_SESSION['name'] . "[{$uid}] said said to [{$to_uid}] :" . $explode_array[1])); return GateWay::sendToUid($to_uid, TextProtocol::encode($_SESSION['name'] . "[{$uid}] said to You :" . $explode_array[1])); } // 群聊 return GateWay::sendToAll(TextProtocol::encode($_SESSION['name'] . "[{$uid}] said :" . $message)); }
/** * 有消息时触发该方法 * @param int $client_id 发消息的client_id * @param string $message 消息 * @return void */ public static function onMessage($client_id, $message) { $message_data = TextProtocol::decode($message); // **************如果没有$_SESSION['name']说明没有设置过用户名,进入设置用户名逻辑************ if (empty($_SESSION['name'])) { $_SESSION['name'] = TextProtocol::decode($message); Gateway::sendToCurrentClient("chat room login success, your client_id is {$client_id}, name is {$_SESSION['name']}\r\nuse client_id:words send message to one user\r\nuse words send message to all\r\n"); // 广播所有用户,xxx come return GateWay::sendToAll(TextProtocol::encode("{$_SESSION['name']}[{$client_id}] come")); } // ********* 进入聊天逻辑 **************** // 判断是否是私聊 $explode_array = explode(':', $message, 2); // 私聊数据格式 client_id:xxxxx if (count($explode_array) > 1) { $to_client_id = (int) $explode_array[0]; GateWay::sendToClient($client_id, TextProtocol::encode($_SESSION['name'] . "[{$client_id}] said said to [{$to_client_id}] :" . $explode_array[1])); return GateWay::sendToClient($to_client_id, TextProtocol::encode($_SESSION['name'] . "[{$client_id}] said to You :" . $explode_array[1])); } // 群聊 return GateWay::sendToAll(TextProtocol::encode($_SESSION['name'] . "[{$client_id}] said :" . $message)); }