php redis 处理websocket聊天记录的实例代码
Redis  /  管理员 发布于 5年前   415
具体代码如下所示: 总结 以上所述是小编给大家介绍的php redis 处理websocket聊天记录的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持! 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';$arr = $chat -> getChatRecord('2', '3', 20);for ($j = 0; $j < count($arr); $j++) {echo $arr[$j] . '';}$chat -> checkUserReadable = false;for ($m = 0; $m < 5; $m++) {$chat -> setChatRecord('2', '3', 'message_' . $m);}echo "";$umsg_1 = $chat -> getUnreadMsgCount(3);echo "Unread message counts ";echo "";print_r($umsg_1);echo "Unread message content ";$umsgContent = $chat -> getUnreadMsg(2, 3);for ($n = 0; $n < count($umsgContent); $n++) {echo $arr[$n] . '';}echo "";$chat -> setUnreadToRead(2, 3);$umsg_2 = $chat -> getUnreadMsgCount(3);echo "";echo "Unread message counts ";echo "";print_r($umsg_2);**/?>
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号