本文实例讲述了PHP+jquery+ajax实现即时聊天功能的方法。分享给大家供大家参考。具体如下:
这是一个简单的利用jquery与php做的一个聊天室的源码,我们这里定时利用ajax读取数据库并进行刷新了,下面直接参上源码,实例代码如下:
index.html页面如下:
<br /> <br /> <br /><meta http-equiv="Content<i style="color:transparent">本#文来源gaodai$ma#com搞$$代**码网$</i><button>搞代gaodaima码</button>-Type" content="text/html; charset=utf-8" /> <br /><title>无标题文档</title> <br /><script src="js/jquery-1.9.1.min.js"></script> <br /><script> <br />var chat = { <br /> init:function(){ <br /> chat.first(); <br /> $('#chat_btn').unbind('click').click(function(){ <br /> chat.send(); <br /> }); <br /> $('#my_chat').keyup(function(){ <br /> if(event.keyCode == 13){ <br /> chat.send(); <br /> } <br /> });<br /> }, <br /> first:function(){ <br /> $.getJSON('data.php',{ <br /> action:'first', <br /> type:'l' <br /> },function(data){ <br /> chat.btn_status._true(); <br /> $('#mwebtime').html(data.time); <br /> $('#chat textarea').val(data.chat); <br /> $('#chat textarea').stop(true,true).animate({scrollTop:9999}, 1); <br /> chat.socket(); <br /> }); <br /> }, <br /> send:function(){ <br /> chat.btn_status._false(); <br /> $.getJSON('send.php',{ <br /> txt:$('#my_chat').val(), <br /> type:'l' <br /> },function(data){ <br /> if(data.status==200){ <br /> chat.btn_status._false(); <br /> $('#my_chat').val(''); <br /> setTimeout(function(){ <br /> chat.btn_status._true(); <br /> },2000); <br /> } <br /> }); <br /> }, <br /> socket:function(){ <br /> $.getJSON('data.php',{ <br /> action:'while', <br /> type:'l' <br /> },function(data){ <br /> $('#mwebtime').html(data.time); <br /> $('#chat textarea').val(data.chat); <br /> $('#chat textarea').stop(true,true).animate({scrollTop:9999}, 1); <br /> chat.socket(); <br /> }); <br /> }, <br /> btn_status:{ <br /> _false:function(){ <br /> $('#chat_btn').html('等待').attr('disabled',true); <br /> }, <br /> _true:function(){ <br /> $('#chat_btn').html('发言').attr('disabled',false); <br /> } <br /> } <br />} <br />chat.init(); <br /></script> <br /> <br /> <br /><body> <br /><div id="chat"> <br /> <textarea style="line-height:20px;font-size:12px;height:100px;width:200px"></textarea> <br /> <BR /> <br /> <br /> <button id="chat_btn" disabled="disabled">发言</button> <br /></div> <br /><div id="mwebtime"></div> <br /> <br />
data.php页面如下:
<?php <br />header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); <br />header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); <br />header("Cache-Control: no-cache, must-revalidate"); <br />header("Pramga: no-cache"); <br />set_time_limit(0); <br />$get = $_GET['action']; <br />$type = $_GET['type']; <br />$file = $type.'.txt'; <br />if(isset($get) && isset($type) && file_exists($file)){ <br /> switch($get){ <br /> case 'first': <br /> $chat = file_get_contents($file); <br /> $json=array( <br /> 'status' => 200, <br /> 'time' => gmdate("s"), <br /> 'chat' => $chat, <br /> ); <br /> echo json_encode($json); <br /> break;<br /> case 'while': <br /> $oldsize = filesize($file); <br /> $newsize = filesize($file); <br /> while(true){ <br /> if($oldsize!=$newsize){ <br /> $chat = file_get_contents($file); <br /> $json=array( <br /> 'status' => 200, <br /> 'time' => gmdate("s"), <br /> 'chat' => $chat, <br /> ); <br /> echo json_encode($json); <br /> exit; <br /> } <br /> clearstatcache(); <br /> $newsize = filesize($file); <br /> usleep(10000); <br /> } <br /> break; <br /> } <br />} <br />?>
send.php页面如下:
<?php <br />$json = array(); <br />$txt = isset($_GET['txt'])?$_GET['txt']:''; <br />$type = isset($_GET['type'])?$_GET['type']:''; <br />if($txt!=''){ <br /> $file = $type.".txt"; <br /> if(file_exists($file)){ <br /> $fp = fopen($file,"a"); <br /> $str = "rn".'Admin:'.$txt; <br /> //$str = $txt."n"//linux; <br /> fwrite($fp, $str); <br /> fclose($fp); <br /> $json['status']=200; <br /> echo json_encode($json); <br /> exit; <br /> } <br />} <br />?>
希望本文所述对大家的php程序设计有所帮助。