用redis 存取workerman-chat聊天记录

xamarin

用存储redis存取5个字段 to from message sent recd 存取的是聊天记录。to 代表发给谁 ,from谁发的 message消息内容 sent发消息的时间 recd代表消息对方看了没有(两种情况1,或者0)
取的使用场景 1用户登录网站成功后,显示未读消息 2用户打开与某个用户的聊天框后,显示最近的20条聊天记录(假定他们曾经聊过天).折腾了几次也没弄好,大家看看有什么建议。

//用户b发送消息到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);
}
8725 4 0
4个回答

xamarin

看到没人回复,在猪八戒找人已经完美解决,聊天记录存取问题。现在共享一下思路。下面是服务商交付的代码。

<?php
ini_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('Ylovey');
    }

    /*
     *发送消息时保存聊天记录
     * 这里用的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;
        $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 . '</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);
 * 
 */
?>
  • 暂无评论
xamarin

我现在正在进行workerman-chat沟通在web网站的具体化,开发的这个joomla的沟通组件叫work,用于商业沟通,测试网站pifaqq.com,正在做workerman-chat具体化的朋友,可以交流一下。

  • 暂无评论
walkor

欢迎交流

  • 暂无评论
ces0135

我也在研究这个聊天记录存储的问题,不过我是用文件保存的

  • 暂无评论
年代过于久远,无法发表回答
🔝