php redis 处理websocket聊天记录的实例代码

前端之家收集整理的这篇文章主要介绍了php redis 处理websocket聊天记录的实例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

具体代码如下所示:

PHP;"> 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,$num) { $keyName = 'rec:' . $this -> getRecKeyName($from,$to); //echo $keyName; $recList = $this -> redis -> lRange($keyName,(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,(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',20); for ($j = 0; $j < count($arr); $j++) { echo $arr[$j] . '
'; } $chat -> checkUserReadable = false; for ($m = 0; $m @H_403_6@ setChatRecord('2','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); * */ ?>

总结

以上所述是小编给大家介绍的PHP redis 处理websocket聊天记录的实例代码,希望对大家有所帮助。程序员遇到问题都会上(编程之家jb51.cc)查找问题解答方法!如果觉得站点还不错,随手转发给程序员朋友一下!

原文链接:https://www.f2er.com/php/15856.html

猜你在找的PHP相关文章