用存儲(chǔ)redis存取5個(gè)字段 to from message sent recd 存取的是聊天記錄。to 代表發(fā)給誰 ,from誰發(fā)的 message消息內(nèi)容 sent發(fā)消息的時(shí)間 recd代表消息對(duì)方看了沒有(兩種情況1,或者0)
取的使用場景 1用戶登錄網(wǎng)站成功后,顯示未讀消息 2用戶打開與某個(gè)用戶的聊天框后,顯示最近的20條聊天記錄(假定他們曾經(jīng)聊過天).折騰了幾次也沒弄好,大家看看有什么建議。
//用戶b發(fā)送消息到a
$arr = array('from'=>'b','message'=>'hello a iam b','sent'=>'1','recd'=>'1');
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$content = json_encode($arr);
$count = $redis->get(a."count");
$count = $count +1;
$redis->set("to".a.$count, $content);
$redis->set(a."count", $count);
//用戶a登錄
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$count = $redis->get(a."count");
for($i=0;$i<$count;$i++){
$content = $redis->get("to".a.$i);
$content = json_decode($content);
$from = $content;
$message = $content;
$sent = $content;
$recd = $content;
//把得到的值使用了就可以了
}
if($count>20){//多余20條記錄,清空記錄
$remove_count = $count -20;
for($i=0;$i<20;$i++){
$content = $redis->get("to".a.$i);
$redis->set("to".a.$i, $content);
$remove_count++;
}
$redis->set(a."count", 20);
}
看到?jīng)]人回復(fù),在豬八戒找人已經(jīng)完美解決,聊天記錄存取問題?,F(xiàn)在共享一下思路。下面是服務(wù)商交付的代碼。
<?php
ini_set('display_errors', 'on');
class chatClass {
private $redis;
public $checkUserReadable = false;
//這個(gè)變量模擬用戶當(dāng)前狀態(tài),是否登錄,是否可查看
//構(gòu)造函數(shù)鏈接redis數(shù)據(jù)庫
public function __construct() {
$this -> redis = new Redis();
$this -> redis -> connect('127.0.0.1', '6379');
$this -> redis -> auth('Ylovey');
}
/*
*發(fā)送消息時(shí)保存聊天記錄
* 這里用的redis存儲(chǔ)是list數(shù)據(jù)類型
* 兩個(gè)人的聊天用一個(gè)list保存
*
* @from 消息發(fā)送者id
* @to 消息接受者id
* @meassage 消息內(nèi)容
*
* 返回值,當(dāng)前聊天的總聊天記錄數(shù)
*/
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) {//消息接受者無法立刻查看時(shí),將消息設(shè)置為未讀
$this -> cacheUnreadMsg($from, $to);
}
return $res;
}
/*
* 獲取聊天記錄
* @from 消息發(fā)送者id
* @to 消息接受者id
* @num 獲取的數(shù)量
*
* 返回值,指定長度的包含聊天記錄的數(shù)組
*/
public function getChatRecord($from, $to, $num) {
$keyName = 'rec:' . $this -> getRecKeyName($from, $to);
//echo $keyName;
$recList = $this -> redis -> lRange($keyName, 0, (int)($num));
return $recList;
}
/*
* 當(dāng)用戶上線時(shí),或點(diǎn)開聊天框時(shí),獲取未讀消息的數(shù)目
* @user 用戶id
*
* 返回值,一個(gè)所有當(dāng)前用戶未讀的消息的發(fā)送者和數(shù)組
* 數(shù)組格式為‘消息發(fā)送者id’=>‘未讀消息數(shù)目’
*
*/
public function getUnreadMsgCount($user) {
return $this -> redis -> hGetAll('unread_' . $user);
}
/*
* 獲取未讀消息的內(nèi)容
* 通過未讀消息數(shù)目,在列表中取得最新的相應(yīng)消息即為未讀
* @from 消息發(fā)送者id
* @to 消息接受者id
*
* 返回值,包括所有未讀消息內(nèi)容的數(shù)組
*
*
*/
public function getUnreadMsg($from, $to) {
$countArr = $this -> getUnreadMsgCount($to);
$count = $countArr;
$keyName = 'rec:' . $this -> getRecKeyName($from, $to);
return $this -> redis -> lRange($keyName, 0, (int)($count));
}
/*
* 將消息設(shè)為已讀
* 當(dāng)一個(gè)用戶打開另一個(gè)用戶的聊天框時(shí),將所有未讀消息設(shè)為已讀
* 清楚未讀消息中的緩存
* @from 消息發(fā)送者id
* @to 消息接受者id
*
* 返回值,成功將未讀消息設(shè)為已讀則返回true,沒有未讀消息則返回false
*/
public function setUnreadToRead($from, $to) {
$res = $this -> redis -> hDel('unread_' . $to, $from);
return (bool)$res;
}
/*
* 當(dāng)用戶不在線時(shí),或者當(dāng)前沒有立刻接收消息時(shí),緩存未讀消息,將未讀消息的數(shù)目和發(fā)送者信息存到一個(gè)與接受者關(guān)聯(lián)的hash數(shù)據(jù)中
*
* @from 發(fā)送消息的用戶id
* @to 接收消息的用戶id
*
* 返回值,當(dāng)前兩個(gè)用戶聊天中的未讀消息
*
*/
private function cacheUnreadMsg($from, $to) {
return $this -> redis -> hIncrBy('unread_' . $to, $from, 1);
}
/*生成聊天記錄的鍵名,即按大小規(guī)則將兩個(gè)數(shù)字排序
* @from 消息發(fā)送者id
* @to 消息接受者id
*
*
*/
private function getRecKeyName($from, $to) {
return ($from > $to) ? $to . '_' . $from : $from . '_' . $to;
}
}
/*
* 下面為測試用的代碼 ,偽造數(shù)據(jù)模擬場景
* 假定有兩個(gè)用戶id為2和3 ,2 向 3 發(fā)送消息
*
$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 . '</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 . '</br>';
}
echo "</br>";
$chat -> setUnreadToRead(2, 3);
$umsg_2 = $chat -> getUnreadMsgCount(3);
echo "</br>";
echo "Unread message counts ";
echo "</br>";
print_r($umsg_2);
*
*/
?>
我現(xiàn)在正在進(jìn)行workerman-chat溝通在web網(wǎng)站的具體化,開發(fā)的這個(gè)joomla的溝通組件叫work,用于商業(yè)溝通,測試網(wǎng)站pifaqq.com,正在做workerman-chat具體化的朋友,可以交流一下。