memcache ajax聊天核心

 

 

PHP代码
  1. function chat_xzy()   
  2.     {   
  3.        if(!$totalnum=cache("chatMsgTotalNum")) $totalnum=cache("chatMsgTotalNum",1,3600*24*50);   
  4.           
  5.         if(isset($_GET[‘ajax’]) && $_GET[‘ajax’]==‘userlist’){   
  6.             $json[‘data’]=getUserOL();   
  7.             $json[‘olusernum’]=olNum();   
  8.             echo json_encode($json);return true;   
  9.         }elseif($_GET[‘ajax’]==‘sendmsg’){   
  10.             $add[‘background’]=strip_tags($_POST[‘background’]);   
  11.             $add[‘color’]=strip_tags($_POST[‘color’]);   
  12.             $add[‘uid’]=$_SESSION[‘user’][‘uid’];   
  13.             $add[‘nickname’]=$_SESSION[‘user’][‘name’];   
  14.             $add[‘touid’]=$_POST[‘touid’];   
  15.             if(!emptyempty($add[‘touid’])){   
  16.                 $a_=db("admin");   
  17.                 $touser=$a_->id($add[‘touid’])->find();   
  18.                 $add[‘tonickname’]=$touser[‘name’];   
  19.             }else{   
  20.                 $add[‘tonickname’]=;   
  21.             }   
  22.             $add[‘msg’]=strip_tags($_POST[‘msg’]);   
  23.             $add[‘time’]=date(‘m-d H:i:s’);   
  24.             $add[‘touid’]=$_POST[‘touid’];   
  25.               
  26.             $totalnum++;   
  27.             cache("chatMsgTotalNum",$totalnum,3600*24*365);   
  28.             cache(‘chatMsgList’.$totalnum,$add,3600*24);   
  29.             $json[‘status’]=0;   
  30.             echo json_encode($json);return true;   
  31.         }elseif($_GET[‘ajax’]==‘showmsg’){   
  32.             if(!$mylastid=cache("chatMsgTotalMy".$_SESSION[‘user’][‘uid’])) $mylastid=cache("chatMsgTotalMy".$_SESSION[‘user’][‘uid’],1,3600*24);   
  33.             $n=$totalnum$mylastid;   
  34.             $list=array();   
  35.             if($n>0){   
  36.                 for($i=1;$i<=$n;$i++){   
  37.                     $x=$mylastid+$i;   
  38.                     $cTmp= cache(‘chatMsgList’.$x);   
  39.                     if(emptyempty($cTmp[‘touid’]) || $cTmp[‘touid’]==$_SESSION[‘user’][‘uid’]) $list[]=$cTmp;   
  40.                 }   
  41.             }   
  42.             cache("chatMsgTotalMy".$_SESSION[‘user’][‘uid’],$totalnum,3600*24);   
  43.             if(!emptyempty($list)){   
  44.                 $json[‘data’]=$list;   
  45.                 $json[‘status’]=0;   
  46.             }else{   
  47.                 $json[‘status’]=1;   
  48.             }   
  49.             echo json_encode($json);return true;   
  50.         }   
  51.         $this->view();   
  52.         $mylastid=cache("chatMsgTotalMy".$_SESSION[‘user’][‘uid’],1,3600*24);   
  53.     }  

 

效果:

 

森.png