php redis 處理websocket聊天記錄


  1 <?php
  2 ini_set('display_errors', 'on');
  3 
  4 class chatClass {
  5     private $redis;
  6 
  7     //這個變量模擬用戶當前狀態,是否登錄,是否可查看
  8     public $checkUserReadable = false;
  9 
 10     //構造函數鏈接redis數據庫
 11     public function __construct() {
 12         $this -> redis = new Redis();
 13         $this -> redis -> connect('127.0.0.1', '6379');
 14         $this -> redis -> auth('***cnblogs.com/handle');
 15     }
 16 
 17     /*
 18     發送消息時保存聊天記錄
 19     * 這里用的redis存儲是list數據類型
 20     * 兩個人的聊天用一個list保存
 21     *
 22     * @from 消息發送者id
 23     * @to 消息接受者id
 24     * @meassage 消息內容
 25     *
 26     * 返回值,當前聊天的總聊天記錄數
 27     */
 28     public function setChatRecord($from, $to, $message) {
 29         $data = array('from' => $from, 'to' => $to, 'message' => $message, 'sent' => time()/*, 'recd' => 0*/);
 30         $value = json_encode($data);
 31         //生成json字符串
 32         $keyName = 'rec:' . $this -> getRecKeyName($from, $to);
 33         //echo $keyName;
 34         $res = $this -> redis -> lPush($keyName, $value);
 35         if (!$this -> checkUserReadable) {//消息接受者無法立刻查看時,將消息設置為未讀
 36             $this -> cacheUnreadMsg($from, $to);
 37         }
 38         return $res;
 39     }
 40 
 41     /*
 42     * 獲取聊天記錄
 43     * @from 消息發送者id
 44     * @to 消息接受者id
 45     * @num 獲取的數量
 46     *
 47     * 返回值,指定長度的包含聊天記錄的數組
 48     */
 49     public function getChatRecord($from, $to, $num) {
 50         $keyName = 'rec:' . $this -> getRecKeyName($from, $to);
 51         //echo $keyName;
 52         $recList = $this -> redis -> lRange($keyName, 0, (int)($num));
 53         return $recList;
 54     }
 55 
 56     /*
 57     * 當用戶上線時,或點開聊天框時,獲取未讀消息的數目
 58     * @user 用戶id
 59     *
 60     * 返回值,一個所有當前用戶未讀的消息的發送者和數組
 61     * 數組格式為‘消息發送者id’=>‘未讀消息數目’
 62     *
 63     */
 64     public function getUnreadMsgCount($user) {
 65         return $this -> redis -> hGetAll('unread_' . $user);
 66     }
 67 
 68     /*
 69     * 獲取未讀消息的內容
 70     * 通過未讀消息數目,在列表中取得最新的相應消息即為未讀
 71     * @from 消息發送者id
 72     * @to 消息接受者id
 73     *
 74     * 返回值,包括所有未讀消息內容的數組
 75     *
 76     *
 77     */
 78     public function getUnreadMsg($from, $to) {
 79         $countArr = $this -> getUnreadMsgCount($to);
 80         $count = $countArr[$from];
 81         $keyName = 'rec:' . $this -> getRecKeyName($from, $to);
 82         return $this -> redis -> lRange($keyName, 0, (int)($count));
 83     }
 84 
 85     /*
 86     * 將消息設為已讀
 87     * 當一個用戶打開另一個用戶的聊天框時,將所有未讀消息設為已讀
 88     * 清楚未讀消息中的緩存
 89     * @from 消息發送者id
 90     * @to 消息接受者id
 91     *
 92     * 返回值,成功將未讀消息設為已讀則返回true,沒有未讀消息則返回false
 93     */
 94 
 95     public function setUnreadToRead($from, $to) {
 96         $res = $this -> redis -> hDel('unread_' . $to, $from);
 97         return (bool)$res;
 98     }
 99 
100     /*
101     * 當用戶不在線時,或者當前沒有立刻接收消息時,緩存未讀消息,將未讀消息的數目和發送者信息存到一個與接受者關聯的hash數據中
102     *
103     * @from 發送消息的用戶id
104     * @to 接收消息的用戶id
105     *
106     * 返回值,當前兩個用戶聊天中的未讀消息
107     *
108     */
109     private function cacheUnreadMsg($from, $to) {
110         return $this -> redis -> hIncrBy('unread_' . $to, $from, 1);
111     }
112 
113     /*生成聊天記錄的鍵名,即按大小規則將兩個數字排序
114     * @from 消息發送者id
115     * @to 消息接受者id
116     *
117     *
118     */
119     private function getRecKeyName($from, $to) {
120         return ($from > $to) ? $to . '_' . $from : $from . '_' . $to;
121     }
122 
123 }
124 
125 /*
126 * 下面為測試用的代碼 ,偽造數據模擬場景
127 * 假定有兩個用戶id為2和3 ,2 向 3 發送消息
128 *
129 
130 $chat = new chatClass();
131 
132 $chat -> checkUserReadable = true;
133 for ($i = 0; $i < 20; $i++) {
134 $chat -> setChatRecord('2', '3', 'message_' . $i);
135 }
136 
137 echo 'get 20 chat records</br>';
138 $arr = $chat -> getChatRecord('2', '3', 20);
139 for ($j = 0; $j < count($arr); $j++) {
140 echo $arr[$j] . '</br>';
141 }
142 
143 $chat -> checkUserReadable = false;
144 
145 for ($m = 0; $m < 5; $m++) {
146 $chat -> setChatRecord('2', '3', 'message_' . $m);
147 }
148 
149 echo "</br>";
150 $umsg_1 = $chat -> getUnreadMsgCount(3);
151 echo "Unread message counts ";
152 echo "</br>";
153 print_r($umsg_1);
154 echo "Unread message content </br> ";
155 $umsgContent = $chat -> getUnreadMsg(2, 3);
156 for ($n = 0; $n < count($umsgContent); $n++) {
157 echo $arr[$n] . '</br>';
158 }
159 echo "</br>";
160 $chat -> setUnreadToRead(2, 3);
161 $umsg_2 = $chat -> getUnreadMsgCount(3);
162 echo "</br>";
163 echo "Unread message counts ";
164 echo "</br>";
165 print_r($umsg_2);
166 *
167 */
168 ?>

歡迎反饋交流

 

 

 

如果本文章已幫助到您!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM