聊天室的实现(消息实时刷新)

简单的对话框:实现消息的两秒一刷新,获取消息记录;

页面加载时,消息框中的滑块处于最低端,向上滑动可以查看历史信息,此时刷新信息的时候,不在刷新滑块,使其仍处于最底层,也就是说,当滑块滚动的时候,不在受实时刷新的控制。当输入新的信息的时候,启动滑块的刷新,使其刷新到最底层(当前DIV的最底层)。另外,内容发送会显示发送状态,2秒后自动消失。

代码实现:

index.html

  1. <html>  
  2.    <head>  
  3.       <title>聊天室</title>   
  4.       <meta http-equiv="content-type" content="text/html;charset=utf-8">  
  5.       <script type="text/javascript">  
  6.             var maxID=0;  
  7.             var kongzhi = 1;  
  8.             function show(){  
  9.                 var xhr = new XMLHttpRequest();  
  10.                 xhr.onreadystatechange = function (){  
  11.                    if(xhr.readyState==4){  
  12.                        //alert(xhr.responseText);  
  13.                        eval("var jn_info = "+xhr.responseText);  
  14.                        var s="";  
  15.                        for(var i=0 ; i<jn_info.length;i++){  
  16.                            s+="<p style='color #233434'>"+jn_info[i].from;  
  17.                            s+="对"+jn_info[i].to+"说:"+jn_info[i].content;  
  18.                            s+="</p>";    
  19.                            maxID = jn_info[i].id;   
  20.                        }  
  21.                     var showview=document.getElementById('view');  
  22.                     showview.innerHTML += s;  
  23.                     if(kongzhi==1){//控制加载时,消息内容处于最底层,而且只进行一次  
  24.                         showview.scrollTop=showview.scrollHeight;     
  25.                         kongzhi++;  
  26.                     }else{  
  27.                         //  
  28.                     }  
  29.                       
  30.                    }  
  31.                 }  
  32.                 xhr.open('get','./data.php?maxID='+maxID);  
  33.                 xhr.send(null);  
  34.             }  
  35.             window.onloadfunction(){  
  36.                 show();  
  37.                 //轮转查询  
  38.                 setInterval("show()",2000);  
  39.             }  
  40.               
  41.       </script>  
  42.    </head>  
  43.    <body>  
  44.         <div id ="view" style="border:1px solid silver; margin:0 auto; width:400px; height:500px; overflow-y: scroll; word-break: break-all;word-wrap: break-word;">  
  45.         <h1>欢迎光临聊天室</h1>  
  46.         <p style="color:red;">小编祝大家幸福</p>  
  47.         </div>  
  48.         <div style=" margin:0 auto; border:1px solid silver; width:400px ;height:60px;" onmousewheel="stop()">  
  49.             <form id="form">   
  50.                            <input type="hidden" name="from" value="周杰伦">  
  51.                            <input type="hidden" name="to" value="李长江">  
  52.                 聊天内容:<input type="text" name="content" id="content"><input type="button" name="submit" value="发送" onClick="send()">  
  53.                  <span id="re"></span>  
  54.             </form>  
  55.         </div>  
  56.    </body>  
  57. </html>  
  58. <script type="text/javascript">  
  59.     function send(){  
  60.         var showview=document.getElementById('view');  
  61.         var fm = document.getElementById('form');//不要通过TagName获取表单  
  62.         var fd = new FormData(fm);  
  63.         var xhr = new XMLHttpRequest();  
  64.         xhr.onreadystatechange = function (){  
  65.            if(xhr.readyState==4){  
  66.                    //alert(xhr.responseText);  
  67.                   document.getElementById('re').innerHTML=xhr.responseText;  
  68.                   document.getElementById('content').value="";  
  69.                   setTimeout(function(){hidden()},2000)  
  70.                   }  
  71.            }  
  72.         xhr.open('post','./action.php');  
  73.         xhr.send(fd);  
  74.         showview.scrollTop=showview.scrollHeight;  
  75.     }  
  76.     function hidden(){  
  77.         document.getElementById('re').innerHTML="";  
  78.     }  
  79.     function stop(){  
  80.         showview.scrollTop=showview.scrollHeight;  
  81.     }  
  82. </script>  
data.php

  1. <?php  
  2.    error_reporting(0);  
  3.    $conn=@mysql_connect("localhost","root" ,"guo941102");  
  4.    mysql_select_db('test',$conn);  
  5.    mysql_query("set names utf8") or die(mysql_errno());  
  6.    $maxID= $_GET['maxID'];  
  7.    $sql="select * from msg where id >'$maxID'";  
  8.    $res=mysql_query($sql);  
  9.    $infoarray();  
  10.    while($row=mysql_fetch_assoc($res)){  
  11.       // $temp=array();  
  12.       // foreach($row as $key => $value){  
  13.         //   $temp[$key]=urlencode($value);  
  14.       // }  
  15.       // $info[]=$temp;  
  16.       $info[]= $row;  
  17.    }  
  18.    //通过json格式传递数据  
  19.   // var_dump($info);  
  20.    //  
  21. //   echo urldecode(json_encode($info));   
  22.      echo json_encode($info);  
  23.   
  24. ?>  
action.php

  1. <?php  
  2.     header("Content-type: text/html; charset=utf-8");   
  3.     error_reporting(0);  
  4.     $conn=@mysql_connect("localhost","root" ,"guo941102");  
  5.     mysql_select_db('test',$conn);  
  6.     mysql_query("set names utf8") or die(mysql_errno());  
  7.     //print_r($_POST);  
  8.     $from    =$_POST['from'];  
  9.     $to      =$_POST['to'];  
  10.     $content =$_POST['content'];  
  11.     $sql = "insert into msg values(null,'$from','$to','$content',now())";  
  12.     if(mysql_query($sql)){  
  13.        echo "发表成功";  
  14.     }  
  15.     else{  
  16.         echo "发表失败";  
  17.     }  
  18. ?>  
只是为了功能的实现,所以界面并不是那么好看。欢迎建议。。。

简单的对话框:实现消息的两秒一刷新,获取消息记录;

页面加载时,消息框中的滑块处于最低端,向上滑动可以查看历史信息,此时刷新信息的时候,不在刷新滑块,使其仍处于最底层,也就是说,当滑块滚动的时候,不在受实时刷新的控制。当输入新的信息的时候,启动滑块的刷新,使其刷新到最底层(当前DIV的最底层)。另外,内容发送会显示发送状态,2秒后自动消失。

代码实现:

index.html

  1. <html>  
  2.    <head>  
  3.       <title>聊天室</title>   
  4.       <meta http-equiv="content-type" content="text/html;charset=utf-8">  
  5.       <script type="text/javascript">  
  6.             var maxID=0;  
  7.             var kongzhi = 1;  
  8.             function show(){  
  9.                 var xhr = new XMLHttpRequest();  
  10.                 xhr.onreadystatechange = function (){  
  11.                    if(xhr.readyState==4){  
  12.                        //alert(xhr.responseText);  
  13.                        eval("var jn_info = "+xhr.responseText);  
  14.                        var s="";  
  15.                        for(var i=0 ; i<jn_info.length;i++){  
  16.                            s+="<p style='color #233434'>"+jn_info[i].from;  
  17.                            s+="对"+jn_info[i].to+"说:"+jn_info[i].content;  
  18.                            s+="</p>";    
  19.                            maxID = jn_info[i].id;   
  20.                        }  
  21.                     var showview=document.getElementById('view');  
  22.                     showview.innerHTML += s;  
  23.                     if(kongzhi==1){//控制加载时,消息内容处于最底层,而且只进行一次  
  24.                         showview.scrollTop=showview.scrollHeight;     
  25.                         kongzhi++;  
  26.                     }else{  
  27.                         //  
  28.                     }  
  29.                       
  30.                    }  
  31.                 }  
  32.                 xhr.open('get','./data.php?maxID='+maxID);  
  33.                 xhr.send(null);  
  34.             }  
  35.             window.onloadfunction(){  
  36.                 show();  
  37.                 //轮转查询  
  38.                 setInterval("show()",2000);  
  39.             }  
  40.               
  41.       </script>  
  42.    </head>  
  43.    <body>  
  44.         <div id ="view" style="border:1px solid silver; margin:0 auto; width:400px; height:500px; overflow-y: scroll; word-break: break-all;word-wrap: break-word;">  
  45.         <h1>欢迎光临聊天室</h1>  
  46.         <p style="color:red;">小编祝大家幸福</p>  
  47.         </div>  
  48.         <div style=" margin:0 auto; border:1px solid silver; width:400px ;height:60px;" onmousewheel="stop()">  
  49.             <form id="form">   
  50.                            <input type="hidden" name="from" value="周杰伦">  
  51.                            <input type="hidden" name="to" value="李长江">  
  52.                 聊天内容:<input type="text" name="content" id="content"><input type="button" name="submit" value="发送" onClick="send()">  
  53.                  <span id="re"></span>  
  54.             </form>  
  55.         </div>  
  56.    </body>  
  57. </html>  
  58. <script type="text/javascript">  
  59.     function send(){  
  60.         var showview=document.getElementById('view');  
  61.         var fm = document.getElementById('form');//不要通过TagName获取表单  
  62.         var fd = new FormData(fm);  
  63.         var xhr = new XMLHttpRequest();  
  64.         xhr.onreadystatechange = function (){  
  65.            if(xhr.readyState==4){  
  66.                    //alert(xhr.responseText);  
  67.                   document.getElementById('re').innerHTML=xhr.responseText;  
  68.                   document.getElementById('content').value="";  
  69.                   setTimeout(function(){hidden()},2000)  
  70.                   }  
  71.            }  
  72.         xhr.open('post','./action.php');  
  73.         xhr.send(fd);  
  74.         showview.scrollTop=showview.scrollHeight;  
  75.     }  
  76.     function hidden(){  
  77.         document.getElementById('re').innerHTML="";  
  78.     }  
  79.     function stop(){  
  80.         showview.scrollTop=showview.scrollHeight;  
  81.     }  
  82. </script>  
data.php

  1. <?php  
  2.    error_reporting(0);  
  3.    $conn=@mysql_connect("localhost","root" ,"guo941102");  
  4.    mysql_select_db('test',$conn);  
  5.    mysql_query("set names utf8") or die(mysql_errno());  
  6.    $maxID= $_GET['maxID'];  
  7.    $sql="select * from msg where id >'$maxID'";  
  8.    $res=mysql_query($sql);  
  9.    $infoarray();  
  10.    while($row=mysql_fetch_assoc($res)){  
  11.       // $temp=array();  
  12.       // foreach($row as $key => $value){  
  13.         //   $temp[$key]=urlencode($value);  
  14.       // }  
  15.       // $info[]=$temp;  
  16.       $info[]= $row;  
  17.    }  
  18.    //通过json格式传递数据  
  19.   // var_dump($info);  
  20.    //  
  21. //   echo urldecode(json_encode($info));   
  22.      echo json_encode($info);  
  23.   
  24. ?>  
action.php

  1. <?php  
  2.     header("Content-type: text/html; charset=utf-8");   
  3.     error_reporting(0);  
  4.     $conn=@mysql_connect("localhost","root" ,"guo941102");  
  5.     mysql_select_db('test',$conn);  
  6.     mysql_query("set names utf8") or die(mysql_errno());  
  7.     //print_r($_POST);  
  8.     $from    =$_POST['from'];  
  9.     $to      =$_POST['to'];  
  10.     $content =$_POST['content'];  
  11.     $sql = "insert into msg values(null,'$from','$to','$content',now())";  
  12.     if(mysql_query($sql)){  
  13.        echo "发表成功";  
  14.     }  
  15.     else{  
  16.         echo "发表失败";  
  17.     }  
  18. ?>  
只是为了功能的实现,所以界面并不是那么好看。欢迎建议。。。

猜你喜欢

转载自blog.csdn.net/php_lzr/article/details/78060025