具體代碼如下所示:
<?phpini_set('display_errors', 'on');class chatClass { private $redis; //這個變量模擬用戶當前狀態,是否登錄,是否可查看 public $checkUserReadable = false; //構造函數鏈接redis數據庫 public function __construct() { $this -> redis = new Redis(); $this -> redis -> connect('127.0.0.1', '6379'); $this -> redis -> auth('***cnblogs.com/handle'); } /* 發送消息時保存聊天記錄 * 這里用的redis存儲是list數據類型 * 兩個人的聊天用一個list保存 * * @from 消息發送者id * @to 消息接受者id * @meassage 消息內容 * * 返回值,當前聊天的總聊天記錄數 */ public function setChatRecord($from, $to, $message) { $data = array('from' => $from, 'to' => $to, 'message' => $message, 'sent' => time()/*, 'recd' => 0*/); $value = json_encode($data); //生成json字符串 $keyName = 'rec:' . $this -> getRecKeyName($from, $to); //echo $keyName; $res = $this -> redis -> lPush($keyName, $value); if (!$this -> checkUserReadable) {//消息接受者無法立刻查看時,將消息設置為未讀 $this -> cacheUnreadMsg($from, $to); } return $res; } /* * 獲取聊天記錄 * @from 消息發送者id * @to 消息接受者id * @num 獲取的數量 * * 返回值,指定長度的包含聊天記錄的數組 */ public function getChatRecord($from, $to, $num) { $keyName = 'rec:' . $this -> getRecKeyName($from, $to); //echo $keyName; $recList = $this -> redis -> lRange($keyName, 0, (int)($num)); return $recList; } /* * 當用戶上線時,或點開聊天框時,獲取未讀消息的數目 * @user 用戶id * * 返回值,一個所有當前用戶未讀的消息的發送者和數組 * 數組格式為‘消息發送者id'=>‘未讀消息數目' * */ public function getUnreadMsgCount($user) { return $this -> redis -> hGetAll('unread_' . $user); } /* * 獲取未讀消息的內容 * 通過未讀消息數目,在列表中取得最新的相應消息即為未讀 * @from 消息發送者id * @to 消息接受者id * * 返回值,包括所有未讀消息內容的數組 * * */ public function getUnreadMsg($from, $to) { $countArr = $this -> getUnreadMsgCount($to); $count = $countArr[$from]; $keyName = 'rec:' . $this -> getRecKeyName($from, $to); return $this -> redis -> lRange($keyName, 0, (int)($count)); } /* * 將消息設為已讀 * 當一個用戶打開另一個用戶的聊天框時,將所有未讀消息設為已讀 * 清楚未讀消息中的緩存 * @from 消息發送者id * @to 消息接受者id * * 返回值,成功將未讀消息設為已讀則返回true,沒有未讀消息則返回false */ public function setUnreadToRead($from, $to) { $res = $this -> redis -> hDel('unread_' . $to, $from); return (bool)$res; } /* * 當用戶不在線時,或者當前沒有立刻接收消息時,緩存未讀消息,將未讀消息的數目和發送者信息存到一個與接受者關聯的hash數據中 * * @from 發送消息的用戶id * @to 接收消息的用戶id * * 返回值,當前兩個用戶聊天中的未讀消息 * */ private function cacheUnreadMsg($from, $to) { return $this -> redis -> hIncrBy('unread_' . $to, $from, 1); } /*生成聊天記錄的鍵名,即按大小規則將兩個數字排序 * @from 消息發送者id * @to 消息接受者id * * */ private function getRecKeyName($from, $to) { return ($from > $to) ? $to . '_' . $from : $from . '_' . $to; }}/** 下面為測試用的代碼 ,偽造數據模擬場景* 假定有兩個用戶id為2和3 ,2 向 3 發送消息*$chat = new chatClass();$chat -> checkUserReadable = true;for ($i = 0; $i < 20; $i++) {$chat -> setChatRecord('2', '3', 'message_' . $i);}echo 'get 20 chat records</br>';$arr = $chat -> getChatRecord('2', '3', 20);for ($j = 0; $j < count($arr); $j++) {echo $arr[$j] . '</br>';}$chat -> checkUserReadable = false;for ($m = 0; $m < 5; $m++) {$chat -> setChatRecord('2', '3', 'message_' . $m);}echo "</br>";$umsg_1 = $chat -> getUnreadMsgCount(3);echo "Unread message counts ";echo "</br>";print_r($umsg_1);echo "Unread message content </br> ";$umsgContent = $chat -> getUnreadMsg(2, 3);for ($n = 0; $n < count($umsgContent); $n++) {echo $arr[$n] . '</br>';}echo "</br>";$chat -> setUnreadToRead(2, 3);$umsg_2 = $chat -> getUnreadMsgCount(3);echo "</br>";echo "Unread message counts ";echo "</br>";print_r($umsg_2);**/?>
總結
以上所述是小編給大家介紹的php redis 處理websocket聊天記錄的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選