PHP+ajax chat room source code! Supports both long round robin and scheduled requests_PHP Tutorial

WBOY
Release: 2016-07-13 17:17:10
Original
1021 people have browsed it


 

 

 

 

<span   1</span> <span var</span> lastID = "1";<span //</span><span 声明上次取回的消息的ID</span>
<span   2</span> <span var</span> isposted = <span false</span><span ;
</span><span   3</span> <span var</span> mGetTime;<span //</span><span 设置setTimeout的返回值
</span><span   4</span> <span // 通过封装getAjax()方法创建XMLHTTPRequest对象</span>
<span   5</span>  <span function</span> <span trim</span>(str){ <span //</span><span 删除左右两端的空格</span>
<span   6</span>  <span return</span> str.replace(/(^\s*)|(\s*$)/g, ""<span );
</span><span   7</span> <span }
</span><span   8</span> 
<span   9</span> <span function</span><span  getPlainTxt() {
</span><span  10</span> <span var</span> str=UE.getEditor('myEditor').<span getContent()
</span><span  11</span> str=<span trim</span><span (str);
</span><span  12</span> $('#mess').<span html(str);
</span><span  13</span> <span }
</span><span  14</span> 
<span  15</span> <span function</span><span  setContentmess() {
</span><span  16</span> UE.getEditor('myEditor').setContent('', <span false</span><span );
</span><span  17</span> <span return</span><span ;
</span><span  18</span> <span }
</span><span  19</span> 
<span  20</span> <span //</span><span 获取JSURL后面的参数</span>
<span  21</span> <span function</span> _getArgs(name,<span _number){
</span><span  22</span> <span var</span> jsFileName =<span  name;
</span><span  23</span> <span var</span> rName = <span new</span> RegExp(jsFileName+"(\\?(.*))?$"<span )
</span><span  24</span> <span var</span> jss=document.getElementsByTagName('script'<span );
</span><span  25</span> <span for</span> (<span var</span> i = 0;i < jss.length; i++<span ){
</span><span  26</span>   <span var</span> j =<span  jss[i];
</span><span  27</span>   <span if</span> (j.src&&j.src.<span match(rName)){
</span><span  28</span>     <span var</span> oo = j.src.match(rName)[2<span ];
</span><span  29</span>     <span if</span> (oo&&(t = oo.match(/([^&=]+)=([^=&]+)/<span g))){
</span><span  30</span>         <span for</span> (<span var</span> l = 0; l < t.length; l++<span ){
</span><span  31</span>             r =<span  t[l];
</span><span  32</span>             <span var</span> tt = r.match(/([^&=]+)=([^=&]+)/<span );
</span><span  33</span>             <span if</span><span  (tt)
</span><span  34</span>             <span return</span><span  tt[_number];
</span><span  35</span>                 <span //</span><span document.write('参数:' + tt[1] + ',参数值:' + tt[2] + '<br />');</span>
<span  36</span> <span         }
</span><span  37</span> <span     }
</span><span  38</span> <span   }
</span><span  39</span> <span }
</span><span  40</span> <span }
</span><span  41</span> 
<span  42</span> 
<span  43</span> <span function</span><span  user_to(){
</span><span  44</span> <span var</span> userid1=$('#userid1').<span val();
</span><span  45</span>     <span if</span>(userid1=='所有人'<span ){
</span><span  46</span> alert('请选择聊天对象哦!\n\n点击用户名称即可!'<span );
</span><span  47</span> $("#userid2").attr("checked",<span false</span><span );
</span><span  48</span> <span     }
</span><span  49</span> <span return</span> <span false</span><span ;
</span><span  50</span> <span }
</span><span  51</span> 
<span  52</span> 
<span  53</span> 
<span  54</span> 
<span  55</span> 
<span  56</span> <span //</span><span 点击选择用户</span>
<span  57</span> <span function</span><span  getFirend(name) {
</span><span  58</span>     <span var</span><span  new_name;
</span><span  59</span>     <span if</span>(name.length > 1 && name!="所有人"<span ) {
</span><span  60</span>         $("#userto").<span val(name);
</span><span  61</span>         $("#userid1").attr("checked",<span false</span><span );
</span><span  62</span>         $("#userid2").attr("checked",<span true</span><span );
</span><span  63</span> 
<span  64</span>         <span if</span>(name.length > 8<span ) {
</span><span  65</span>             new_name = name.<span substr</span>(0, 6<span );
</span><span  66</span>         } <span else</span><span  {
</span><span  67</span>             new_name =<span  name;
</span><span  68</span> <span         }
</span><span  69</span>         document.getElementById("tousertitle").innerHTML = "<font color=#ff6600 title=" + new_name + ">" + new_name + "</font>"<span ;
</span><span  70</span>     }<span else</span><span {
</span><span  71</span> 
<span  72</span>         $("#userto").val("所有人"<span );
</span><span  73</span> 
<span  74</span>         $("#userid1").attr("checked",<span true</span><span );
</span><span  75</span>         $("#userid2").attr("checked",<span false</span><span );
</span><span  76</span> 
<span  77</span> 
<span  78</span> 
<span  79</span>         document.getElementById("tousertitle").innerHTML = "用户"<span ;
</span><span  80</span> <span     }
</span><span  81</span> <span }
</span><span  82</span> 
<span  83</span> 
<span  84</span> 
<span  85</span> <span //</span><span 发送消息的方法</span>
<span  86</span> <span function</span><span  sendMess()
</span><span  87</span> <span {
</span><span  88</span> 
<span  89</span> <span    getPlainTxt();
</span><span  90</span>     <span //</span><span 如果消息为空给出提示并返回</span>
<span  91</span>     <span var</span> messvalue  =$("#mess").<span html();
</span><span  92</span>         messvalue=<span trim</span><span (messvalue);
</span><span  93</span> 
<span  94</span>         <span if</span>(messvalue.length>650<span ){
</span><span  95</span>         alert("参与互动提示:\n亲,输入的文字是不是有点多了?!,少说点吧!\n\n复制过来的文字!请先清除格式!"<span );
</span><span  96</span>         <span //</span><span document.getElementById("mess").focus();//把焦点设置到消息输入框</span>
<span  97</span>         <span return</span> <span false</span><span ;
</span><span  98</span> <span         }
</span><span  99</span> 
<span 100</span> 
<span 101</span> 
<span 102</span>         <span if</span>($("#mess").html().length<2<span ){
</span><span 103</span>         alert("参与互动提示:\n请填写您要发表的内容,长度必须2~250个字节!"<span );
</span><span 104</span>         <span //</span><span document.getElementById("mess").focus();//把焦点设置到消息输入框</span>
<span 105</span>         <span return</span> <span false</span><span ;
</span><span 106</span> <span         }
</span><span 107</span> 
<span 108</span>     <span var</span> d = <span new</span> <span Date</span><span ();
</span><span 109</span>         <span var</span> username = $("#username").<span val() ;
</span><span 110</span>         <span var</span> userto   = $("#userto").<span val() ;
</span><span 111</span>         <span var</span> userid   = $("#userid").<span val() ;
</span><span 112</span>         <span //</span><span var mess   = document.getElementById("mess").value ;</span>
<span 113</span>         <span var</span> mess     =<span  messvalue ;
</span><span 114</span>         <span var</span> userpid  = $("#userpid").<span val() ;
</span><span 115</span>         <span var</span> IP       = $("#IP").<span val() ;
</span><span 116</span>         <span var</span> usertype = $("#usertype").<span val() ;
</span><span 117</span> 
<span 118</span>         <span var</span> sendUrl = "username="+username+"&userto="+userto+"&userid="+userid+"&userpid="+userpid+"&mess="+mess +"&IP="+IP +"&usertype="+usertype+"&d="+d.<span getTime();
</span><span 119</span>         <span var</span> sendUrl =<span  encodeURI(sendUrl);
</span><span 120</span>       XMLHttp.sendReq('POST', 'send.php',sendUrl,<span  send_response);
</span><span 121</span> 
<span 122</span> 
<span 123</span> <span }
</span><span 124</span> 
<span 125</span> <span function</span><span  send_response(obj){
</span><span 126</span>     <span var</span> response = obj.<span responseText;
</span><span 127</span>     <span if</span>(response=='ok'<span ){
</span><span 128</span> <span if</span>(!<span isposted){
</span><span 129</span> isposted = <span true</span><span ;
</span><span 130</span> <span getMess();
</span><span 131</span> <span }
</span><span 132</span>       setContentmess();<span //</span><span 设置消息框为空</span>
<span 133</span> <span     }
</span><span 134</span>     <span if</span>(response=='false'<span ){
</span><span 135</span>       alert("发送失败!你的字符含有非法字"<span );
</span><span 136</span> <span getMess();
</span><span 137</span> <span     }
</span><span 138</span> <span }
</span><span 139</span> 
<span 140</span> <span function</span><span  getMess(){
</span><span 141</span> <span var</span> d = <span new</span> <span Date</span><span ();
</span><span 142</span> 
<span 143</span> <span var</span> getUrl = "getmess.php?lastid="+lastID +"&numbermess=80&d="+d.getTime();<span //</span><span 从服务器返回消息的地址</span>
<span 144</span>  $.<span ajax({
</span><span 145</span>                 type:"GET",
<span 146</span>                 dataType:"xml",
<span 147</span>                 url:getUrl,
<span 148</span>                 async: <span true</span>,
<span 149</span>                 timeout:80000,     <span //</span><span ajax请求超时时间80秒</span>
<span 150</span> 
<span 151</span>                 success:<span function</span>(data,<span textStatus){
</span><span 152</span>                     <span //</span><span 从服务器得到数据,显示数据并继续查询</span>
<span 153</span> isposted = <span false</span><span ;
</span><span 154</span> <span var</span> chatEL = $("#chat"<span );
</span><span 155</span> $(data).find("message").<span each</span>(<span function</span>(i,<span va){
</span><span 156</span> <span var</span>  sid      =$(this).find("sid").<span text();
</span><span 157</span> <span var</span>  succ     =$(this).find("succ").<span text();
</span><span 158</span> <span var</span>  umax     =$(this).find("umax").<span text();
</span><span 159</span> <span var</span>  user     =$(this).find("user").<span text();
</span><span 160</span> <span var</span>  userto   =$(this).find("userto").<span text();
</span><span 161</span> <span var</span>  userid   =$(this).find("userid").<span text();
</span><span 162</span> <span var</span>  usersex  =$(this).find("usersex").<span text();
</span><span 163</span> <span var</span>  usertype =$(this).find("usertype").<span text();
</span><span 164</span> <span var</span>  userpic  =$(this).find("userpic").<span text();
</span><span 165</span> <span var</span>  userpid  =$(this).find("userpid").<span text();
</span><span 166</span> <span var</span>  text     =$(this).find("text").<span text();
</span><span 167</span> <span var</span>  <span date</span>     =$(this).find("date").<span text();
</span><span 168</span> <span var</span>  uid          = _getArgs('function.js','2'<span )
</span><span 169</span>  <span //</span><span 有数据</span>
<span 170</span> 
<span 171</span>  <span if</span>(succ==1<span ){
</span><span 172</span>                 <span if</span>(uid>=98<span ){
</span><span 173</span>                 delhtml='<a href="#"  id="delmess" onclick=delmess("'+ sid +'","delmess");>[删除]</a><a href="#"  onclick=delmess("'+ userid +'","delusermess");  id="delusermess">[删除该用户的全部信息]</a>'<span ;
</span><span 174</span>                 }<span else</span><span {
</span><span 175</span>                 delhtml=''<span ;
</span><span 176</span> <span                 }
</span><span 177</span>                 <span if</span>(usertype>=98<span ){
</span><span 178</span>                 <span var</span> chatELinnerHTML = '<div class=chu><ul><li class=chu1><a href=http://www.0917cc.com?ID='+userid+' target=_blank><img src='+userpic+' border=0 width=70 height=70 /></a></li><li class=chu2><a href=http://www.0917cc.com?ID='+userid+' target=_blank><img src=/WebSystems/images/img/icons/gif/group.png /></a> <a href=javascript:void(0) onclick=getFirend("'+ user +'"); ><font class=ChatFont3>管理员(' + user + ')</font></a> <img src=/WebSystems/images/img/icons/gif/male.png /> <a href=javascript:void(0) onclick=getFirend("'+ userto +'");><font class=ChatFont3>' + userto + '</font></a> <font color=#575D69>说</font> <font color=#575D69>' + <span date</span> +'</font><img src=/WebSystems/images/img/icons/gif/07.gif />'+delhtml+'<br>' +text + '</li></ul></div>'<span ;
</span><span 179</span>                 chatEL.<span append(chatELinnerHTML);
</span><span 180</span>                 }<span else</span><span {
</span><span 181</span>                 <span var</span> chatELinnerHTML = '<div class=chu><ul><li class=chu1><a href="#" target=_blank><img src='+userpic+' border=0 width=70 height=70 /></a></li><li class=chu2><a href=http://www.0917cc.com?ID='+userid+' target=_blank><img src=/WebSystems/images/img/icons/gif/group.png /></a> <a href=javascript:void(0) onclick=getFirend("'+ user +'"); ><font class=ChatFont1>(' + user + ')</font></a> <img src=/WebSystems/images/img/icons/gif/male.png /> <a href=javascript:void(0) onclick=getFirend("'+ userto +'");><font class=ChatFont2>' + userto + '</font></a> <font color=#575D69>说</font> <font color=#575D69>' + <span date</span> +'</font>'+delhtml+'<br>' + text+ '</li></ul></div>'<span ;
</span><span 182</span>                 chatEL.<span append(chatELinnerHTML);
</span><span 183</span> <span                 }
</span><span 184</span>     lastID = umax;<span //</span><span 上次消息的ID</span>
<span 185</span>     chatEL.scrollTop(document.getElementById("chat").scrollHeight);<span //</span><span 滚动到最后一条消息</span>
<span 186</span>     <span if</span>(!<span isposted){
</span><span 187</span>     isposted = <span true</span><span ;
</span><span 188</span> <span     getMess();
</span><span 189</span> <span     }
</span><span 190</span> <span }
</span><span 191</span> 
<span 192</span> 
<span 193</span> 
<span 194</span> <span //</span><span 未从服务器得到数据,继续查询</span>
<span 195</span> <span if</span>(succ==0<span ){
</span><span 196</span> lastID = sid;<span //</span><span 上次消息的ID</span>
<span 197</span> chatEL.scrollTop = chatEL.scrollHeight;<span //</span><span 滚动到最后一条消息</span>
<span 198</span> <span if</span>(!<span isposted){
</span><span 199</span> isposted = <span true</span><span ;
</span><span 200</span> <span getMess();
</span><span 201</span> <span }
</span><span 202</span> <span }
</span><span 203</span> <span  });
</span><span 204</span> <span //</span><span each--end</span>
<span 205</span>              },
<span 206</span>              <span //</span><span -success-end
</span><span 207</span> <span              //Ajax请求超时,继续查询</span>
<span 208</span>              error:<span function</span>(XMLHttpRequest,textStatus,<span errorThrown){
</span><span 209</span>                      <span if</span>(textStatus=="timeout"<span ){
</span><span 210</span>                        $("#msg").show().html("您有好长时间没有发言了啊.").fadeOut(3200<span );
</span><span 211</span>                          <span if</span>(!<span isposted){
</span><span 212</span> isposted = <span true</span><span ;
</span><span 213</span> <span getMess();
</span><span 214</span> <span }
</span><span 215</span> <span                      }
</span><span 216</span> <span              }
</span><span 217</span> 
<span 218</span> <span             });
</span><span 219</span> 
<span 220</span> }
Copy after login

 

 

*本聊天系统采用PHP+AJAX+MYSQL开发而成!数据库支持mysql,支持XML,支持txt,支持accsess等数据库,可随意转换
聊天功能:1:表情2:超链接3:发送图片,4发送文件,5字体设置,6涂鸦功能
功能:可以对某个人说话,也可以对所有人说话,支持私聊(vip功能)
支持PHP长连接sleep实现的聊天室源码跟ajax实现的聊天室代码
作者:margin(qq:249189520----qq群:90316079-----账号1:admin密码:admin   账号2:demo 密码:demo

官方网站:http://www.0917cc.com

PHP+ajax聊天室模拟推送http://room.0917y.com/websystems/Chatroom2/

可以在火狐FF下查看连接进程跟下面的有什么不一样
PHP+ajax聊天室http://room.0917y.com/websystems/Chatroom/index.php?pid=25&ppid=64

*模拟推送是根据PHP长连接sleep实现的

不足跟缺点:
一直困惑了我一晚上都没有解决的问题是,在编辑器开头第一个插入空格的话!老是提示错误!好像跟XML规范有关系!
又好像是编辑器ueditor的问题取出来的空格会有c2a0会变成乱码,处理这种情况\u00a0不知道怎么处理
服务器的过滤不行!客户的过滤还不行!请求高手看下....

 

 

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/626624.htmlTechArticlelastID = "1"; isposted = mGetTime; (str){ str.replace(/(^\s*)|(\s*$)/g, "" str=UE.getEditor('myEditor'). str= $('#mess'). UE.getEditor('myEditor').setContent('', _getArgs(name, jsF...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template