查看: 1754|回复: 0

[PHP实例] php+webSoket实现聊天室示例代码(附源码)

发表于 2017-12-12 08:00:01

最近在公司利用直播间搭建一个图文直播间时正好要用到chatsever,研究了一下html5的websocket 实现了双向通信,根据前人的经验折腾了几天弄了个聊天室,实现了发送图片,发送QQ表情,群聊私聊等功能,特地分享给各位新手参考学习,大牛可以忽略。

前端:client.html

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
  6. <title>HTML5 websocket 网页聊天室 javascript php</title>
  7. <style type="text/css">
  8. body,p{margin:0px; padding:0px; font-size:14px; color:#333; font-family:Arial, Helvetica, sans-serif;}
  9. #ltian,.rin{width:98%; margin:5px auto;}
  10. #ltian{border:1px #ccc solid;overflow-y:auto; overflow-x:hidden; position:relative;}
  11. #ct{margin-right:111px; height:100%;overflow-y:auto;overflow-x: hidden;}
  12. #us{width:110px; overflow-y:auto; overflow-x:hidden; float:right; border-left:1px #ccc solid; height:100%; background-color:#F1F1F1;}
  13. #us p{padding:3px 5px; color:#08C; line-height:20px; height:20px; cursor:pointer; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;}
  14. #us p:hover,#us p:active,#us p.ck{background-color:#069; color:#FFF;}
  15. #us p.my:hover,#us p.my:active,#us p.my{color:#333;background-color:transparent;}
  16. button{float:right; width:80px; height:35px; font-size:18px;}
  17. input{width:100%; height:30px; padding:2px; line-height:20px; outline:none; border:solid 1px #CCC;}
  18. .rin p{margin-right:160px;}
  19. .rin span{float:right; padding:6px 5px 0px 5px; position:relative;}
  20. .rin span img{margin:0px 3px; cursor:pointer;}
  21. .rin span form{position:absolute; width:25px; height:25px; overflow:hidden; opacity:0; top:5px; right:5px;}
  22. .rin span input{width:180px; height:25px; margin-left:-160px; cursor:pointer}
  23. #ct p{padding:5px; line-height:20px;}
  24. #ct a{color:#069; cursor:pointer;}
  25. #ct span{color:#999; margin-right:10px;}
  26. .c2{color:#999;}
  27. .c3{background-color:#DBE9EC; padding:5px;}
  28. .qp{position:absolute; font-size:12px; color:#666; top:5px; right:130px; text-decoration:none; color:#069;}
  29. #ems{position:absolute; z-index:5; display:none; top:0px; left:0px; max-width:230px; background-color:#F1F1F1; border:solid 1px #CCC; padding:5px;}
  30. #ems img{width:44px; height:44px; border:solid 1px #FFF; cursor:pointer;}
  31. #ems img:hover,#ems img:active{border-color:#A4B7E3;}
  32. #ems a{color:#069; border-radius:2px; display:inline-block; margin:2px 5px; padding:1px 8px; text-decoration:none; background-color:#D5DFFD;}
  33. #ems a:hover,#ems a:active,#ems a.ck{color:#FFF; background-color:#069;}
  34. .tc{text-align:center; margin-top:5px;}
  35. </style>
  36. </head>
  37. <body>
  38. <div id="ltian">
  39. <div id="us" class="jb"></div>
  40. <div id="ct"></div>
  41. <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="qp" onClick="this.parentNode.children[1].innerHTML=''">清屏</a>
  42. </div>
  43. <div class="rin">
  44. <button id="sd">发送</button>
  45. <span><img src="http://www.yxsss.com/ui/sk/t.png" title="表情" id="imgbq"><img src="http://www.yxsss.com/ui/sk/e.png" title="上传图片"><form><input type="file" title="上传图片" id="upimg"></form></span>
  46. <p><input id="nrong"></p>
  47. </div>
  48. <div id="ems"><p></p><p class="tc"></p></div>
  49. <script>
  50. if(typeof(WebSocket)=='undefined'){
  51. alert('你的浏览器不支持 WebSocket ,推荐使用Google Chrome 或者 Mozilla Firefox');
  52. }
  53. </script>
  54. <script src="http://www.yxsss.com/ui/p/a.js" type="text/javascript"></script>
  55. <script>
  56. (function(){
  57. var key='all',mkey;
  58. var users={};
  59. var url='ws://127.0.0.1:8000';
  60. var so=false,n=false;
  61. var lus=A.$('us'),lct=A.$('ct');
  62. function st(){
  63. var Arr1 = ["聪明的","狡猾的","可爱的","美丽的","狡猾的","善良的","帅气的","逗比的"];
  64. var Arr2 = ["大灰狼","小白兔","母老虎","外星人","皮卡丘","HelloKitty","吴亦凡","薛之谦"];
  65. var ran1 = Math.floor(Math.random() * Arr1.length + 1)-1;
  66. var ran2 = Math.floor(Math.random() * Arr2.length + 1)-1;
  67. var n=Arr1[ran1]+Arr2[ran2];
  68. //以上五行是用来随机生成用户昵称的方法,参考一下 ,如果想自定义用户名可以将以上五行注释,然后以下两行取消注释
  69. //n=prompt('请给自己取一个霸气的名字:');
  70. //n=n.substr(0,16);
  71. //console.log(n);
  72. if(!n){
  73. return ;
  74. }
  75. so=new WebSocket(url);
  76. so.onopen=function(){
  77. if(so.readyState==1){
  78. so.send('type=add&ming='+n);
  79. }
  80. }
  81. so.onclose=function(){
  82. so=false;
  83. lct.appendChild(A.$$('<p class="c2">退出聊天室</p>'));
  84. }
  85. so.onmessage=function(msg){
  86. eval('var da='+msg.data);
  87. var obj=false,c=false;
  88. if(da.type=='add'){
  89. var obj=A.$$('<p>'+da.name+'</p>');
  90. lus.appendChild(obj);
  91. cuser(obj,da.code);
  92. obj=A.$$('<p><span>['+da.time+']</span>欢迎<a>'+da.name+'</a>加入</p>');
  93. c=da.code;
  94. }else if(da.type=='madd'){
  95. mkey=da.code;
  96. da.users.unshift({'code':'all','name':'大家'});
  97. for(var i=0;i<da.users.length;i++){
  98. var obj=A.$$('<p>'+da.users[i].name+'</p>');
  99. lus.appendChild(obj);
  100. if(mkey!=da.users[i].code){
  101. cuser(obj,da.users[i].code);
  102. }else{
  103. obj.className='my';
  104. document.title=da.users[i].name;
  105. }
  106. }
  107. obj=A.$$('<p><span>['+da.time+']</span>欢迎'+da.name+'加入</p>');
  108. users.all.className='ck';
  109. }
  110. if(obj==false){
  111. if(da.type=='rmove'){
  112. var obj=A.$$('<p class="c2"><span>['+da.time+']</span>'+users[da.nrong].innerHTML+'退出聊天室</p>');
  113. lct.appendChild(obj);
  114. users[da.nrong].del();
  115. delete users[da.nrong];
  116. }else{
  117. da.nrong=da.nrong.replace(/{\\(\d+)}/g,function(a,b){
  118. return '<img src="sk/'+b+'.jpg">';
  119. }).replace(/^data\:image\/png;base64\,.{50,}$/i,function(a){
  120. return '<img src="'+a+'">';
  121. });
  122. //da.code 发信息人的code
  123. if(da.code1==mkey){
  124. obj=A.$$('<p class="c3"><span>['+da.time+']</span><a>'+users[da.code].innerHTML+'</a>对我说:'+da.nrong+'</p>');
  125. c=da.code;
  126. }else if(da.code==mkey){
  127. if(da.code1!='all')
  128. obj=A.$$('<p class="c3"><span>['+da.time+']</span>我对<a>'+users[da.code1].innerHTML+'</a>说:'+da.nrong+'</p>');
  129. else
  130. obj=A.$$('<p><span>['+da.time+']</span>我对<a>'+users[da.code1].innerHTML+'</a>说:'+da.nrong+'</p>');
  131. c=da.code1;
  132. }else if(da.code==false){
  133. obj=A.$$('<p><span>['+da.time+']</span>'+da.nrong+'</p>');
  134. }else if(da.code1){
  135. obj=A.$$('<p><span>['+da.time+']</span><a>'+users[da.code].innerHTML+'</a>对'+users[da.code1].innerHTML+'说:'+da.nrong+'</p>');
  136. c=da.code;
  137. }
  138. }
  139. }
  140. if(c){
  141. obj.children[1].onclick=function(){
  142. users[c].onclick();
  143. }
  144. }
  145. lct.appendChild(obj);
  146. lct.scrollTop=Math.max(0,lct.scrollHeight-lct.offsetHeight);
  147. }
  148. }
  149. A.$('sd').onclick=function(){
  150. if(!so){
  151. return st();
  152. }
  153. var da=A.$('nrong').value.trim();
  154. if(da==''){
  155. alert('内容不能为空');
  156. return false;
  157. }
  158. A.$('nrong').value='';
  159. so.send('nr='+esc(da)+'&key='+key);
  160. }
  161. A.$('nrong').onkeydown=function(e){
  162. var e=e||event;
  163. if(e.keyCode==13){
  164. A.$('sd').onclick();
  165. }
  166. }
  167. function esc(da){
  168. da=da.replace(/</g,'<').replace(/>/g,'>').replace(/\"/g,'"');
  169. return encodeURIComponent(da);
  170. }
  171. function cuser(t,code){
  172. users[code]=t;
  173. t.onclick=function(){
  174. t.parentNode.children.rcss('ck','');
  175. t.rcss('','ck');
  176. key=code;
  177. }
  178. }
  179. A.$('ltian').style.height=(document.documentElement.clientHeight - 70)+'px';
  180. st();
  181. var bq=A.$('imgbq'),ems=A.$('ems');
  182. var l=80,r=4,c=5,s=0,p=Math.ceil(l/(r*c));
  183. var pt='sk/';
  184. bq.onclick=function(e){
  185. var e=e||event;
  186. if(!so){
  187. return st();
  188. }
  189. ems.style.display='block';
  190. document.onclick=function(){
  191. gb();
  192. }
  193. ct();
  194. try{e.stopPropagation();}catch(o){}
  195. }
  196. for(var i=0;i<p;i++){
  197. var a=A.$$('<a href="javascript:;" rel="external nofollow" rel="external nofollow" >'+(i+1)+'</a>');
  198. ems.children[1].appendChild(a);
  199. ef(a,i);
  200. }
  201. ems.children[1].children[0].className='ck';
  202. function ct(){
  203. var wz=bq.weiz();
  204. with(ems.style){
  205. top=wz.y-242+'px';
  206. left=wz.x+bq.offsetWidth-235+'px';
  207. }
  208. }
  209. function ef(t,i){
  210. t.onclick=function(e){
  211. var e=e||event;
  212. s=i*r*c;
  213. ems.children[0].innerHTML='';
  214. hh();
  215. this.parentNode.children.rcss('ck','');
  216. this.rcss('','ck');
  217. try{e.stopPropagation();}catch(o){}
  218. }
  219. }
  220. function hh(){
  221. var z=Math.min(l,s+r*c);
  222. for(var i=s;i<z;i++){
  223. var a=A.$$('<img src="'+pt+i+'.jpg">');
  224. hh1(a,i);
  225. ems.children[0].appendChild(a);
  226. }
  227. ct();
  228. }
  229. function hh1(t,i){
  230. t.onclick=function(e){
  231. var e=e||event;
  232. A.$('nrong').value+='{\\'+i+'}';
  233. if(!e.ctrlKey){
  234. gb();
  235. }
  236. try{e.stopPropagation();}catch(o){}
  237. }
  238. }
  239. function gb(){
  240. ems.style.display='';
  241. A.$('nrong').focus();
  242. document.onclick='';
  243. }
  244. hh();
  245. A.on(window,'resize',function(){
  246. A.$('ltian').style.height=(document.documentElement.clientHeight - 70)+'px';
  247. ct();
  248. })
  249. var fimg=A.$('upimg');
  250. var img=new Image();
  251. var dw=400,dh=300;
  252. A.on(fimg,'change',function(ev){
  253. if(!so){
  254. st();
  255. return false;
  256. }
  257. if(key=='all'){
  258. alert('由于资源限制 发图只能私聊');
  259. return false;
  260. }
  261. var f=ev.target.files[0];
  262. if(f.type.match('image.*')){
  263. var r = new FileReader();
  264. r.onload = function(e){
  265. img.setAttribute('src',e.target.result);
  266. };
  267. r.readAsDataURL(f);
  268. }
  269. });
  270. img.onload=function(){
  271. ih=img.height,iw=img.width;
  272. if(iw/ih > dw/dh && iw > dw){
  273. ih=ih/iw*dw;
  274. iw=dw;
  275. }else if(ih > dh){
  276. iw=iw/ih*dh;
  277. ih=dh;
  278. }
  279. var rc = A.$$('canvas');
  280. var ct = rc.getContext('2d');
  281. rc.width=iw;
  282. rc.height=ih;
  283. ct.drawImage(img,0,0,iw,ih);
  284. var da=rc.toDataURL();
  285. so.send('nr='+esc(da)+'&key='+key);
  286. }
  287. })();
  288. </script>
  289. </body>
  290. </html>
复制代码

后端代码:webserver.php

  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3. ob_implicit_flush();
  4. $sk=new Sock('127.0.0.1',8000);
  5. $sk->run();
  6. class Sock{
  7. public $sockets;
  8. public $users;
  9. public $master;
  10. private $sda=array();//已接收的数据
  11. private $slen=array();//数据总长度
  12. private $sjen=array();//接收数据的长度
  13. private $ar=array();//加密key
  14. private $n=array();
  15. public function __construct($address, $port){
  16. $this->master=$this->WebSocket($address, $port);
  17. $this->sockets=array($this->master);
  18. }
  19. function run(){
  20. while(true){
  21. $changes=$this->sockets;
  22. $write=NULL;
  23. $except=NULL;
  24. socket_select($changes,$write,$except,NULL);
  25. foreach($changes as $sock){
  26. if($sock==$this->master){
  27. $client=socket_accept($this->master);
  28. $key=uniqid();
  29. $this->sockets[]=$client;
  30. $this->users[$key]=array(
  31. 'socket'=>$client,
  32. 'shou'=>false
  33. );
  34. }else{
  35. $len=0;
  36. $buffer='';
  37. do{
  38. $l=socket_recv($sock,$buf,1000,0);
  39. $len+=$l;
  40. $buffer.=$buf;
  41. }while($l==1000);
  42. $k=$this->search($sock);
  43. if($len<7){
  44. $this->send2($k);
  45. continue;
  46. }
  47. if(!$this->users[$k]['shou']){
  48. $this->woshou($k,$buffer);
  49. }else{
  50. $buffer = $this->uncode($buffer,$k);
  51. if($buffer==false){
  52. continue;
  53. }
  54. $this->send($k,$buffer);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. function close($k){
  61. socket_close($this->users[$k]['socket']);
  62. unset($this->users[$k]);
  63. $this->sockets=array($this->master);
  64. foreach($this->users as $v){
  65. $this->sockets[]=$v['socket'];
  66. }
  67. $this->e("key:$k close");
  68. }
  69. function search($sock){
  70. foreach ($this->users as $k=>$v){
  71. if($sock==$v['socket'])
  72. return $k;
  73. }
  74. return false;
  75. }
  76. function WebSocket($address,$port){
  77. $server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  78. socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
  79. socket_bind($server, $address, $port);
  80. socket_listen($server);
  81. $this->e('Server Started : '.date('Y-m-d H:i:s'));
  82. $this->e('Listening on : '.$address.' port '.$port);
  83. return $server;
  84. }
  85. function woshou($k,$buffer){
  86. $buf = substr($buffer,strpos($buffer,'Sec-WebSocket-Key:')+18);
  87. $key = trim(substr($buf,0,strpos($buf,"\r\n")));
  88. $new_key = base64_encode(sha1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11",true));
  89. $new_message = "HTTP/1.1 101 Switching Protocols\r\n";
  90. $new_message .= "Upgrade: websocket\r\n";
  91. $new_message .= "Sec-WebSocket-Version: 13\r\n";
  92. $new_message .= "Connection: Upgrade\r\n";
  93. $new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n";
  94. socket_write($this->users[$k]['socket'],$new_message,strlen($new_message));
  95. $this->users[$k]['shou']=true;
  96. return true;
  97. }
  98. function uncode($str,$key){
  99. $mask = array();
  100. $data = '';
  101. $msg = unpack('H*',$str);
  102. $head = substr($msg[1],0,2);
  103. if ($head == '81' && !isset($this->slen[$key])) {
  104. $len=substr($msg[1],2,2);
  105. $len=hexdec($len);
  106. if(substr($msg[1],2,2)=='fe'){
  107. $len=substr($msg[1],4,4);
  108. $len=hexdec($len);
  109. $msg[1]=substr($msg[1],4);
  110. }else if(substr($msg[1],2,2)=='ff'){
  111. $len=substr($msg[1],4,16);
  112. $len=hexdec($len);
  113. $msg[1]=substr($msg[1],16);
  114. }
  115. $mask[] = hexdec(substr($msg[1],4,2));
  116. $mask[] = hexdec(substr($msg[1],6,2));
  117. $mask[] = hexdec(substr($msg[1],8,2));
  118. $mask[] = hexdec(substr($msg[1],10,2));
  119. $s = 12;
  120. $n=0;
  121. }else if($this->slen[$key] > 0){
  122. $len=$this->slen[$key];
  123. $mask=$this->ar[$key];
  124. $n=$this->n[$key];
  125. $s = 0;
  126. }
  127. $e = strlen($msg[1])-2;
  128. for ($i=$s; $i<= $e; $i+= 2) {
  129. $data .= chr($mask[$n%4]^hexdec(substr($msg[1],$i,2)));
  130. $n++;
  131. }
  132. $dlen=strlen($data);
  133. if($len > 255 && $len > $dlen+intval($this->sjen[$key])){
  134. $this->ar[$key]=$mask;
  135. $this->slen[$key]=$len;
  136. $this->sjen[$key]=$dlen+intval($this->sjen[$key]);
  137. $this->sda[$key]=$this->sda[$key].$data;
  138. $this->n[$key]=$n;
  139. return false;
  140. }else{
  141. unset($this->ar[$key],$this->slen[$key],$this->sjen[$key],$this->n[$key]);
  142. $data=$this->sda[$key].$data;
  143. unset($this->sda[$key]);
  144. return $data;
  145. }
  146. }
  147. function code($msg){
  148. $frame = array();
  149. $frame[0] = '81';
  150. $len = strlen($msg);
  151. if($len < 126){
  152. $frame[1] = $len<16?'0'.dechex($len):dechex($len);
  153. }else if($len < 65025){
  154. $s=dechex($len);
  155. $frame[1]='7e'.str_repeat('0',4-strlen($s)).$s;
  156. }else{
  157. $s=dechex($len);
  158. $frame[1]='7f'.str_repeat('0',16-strlen($s)).$s;
  159. }
  160. $frame[2] = $this->ord_hex($msg);
  161. $data = implode('',$frame);
  162. return pack("H*", $data);
  163. }
  164. function ord_hex($data) {
  165. $msg = '';
  166. $l = strlen($data);
  167. for ($i= 0; $i<$l; $i++) {
  168. $msg .= dechex(ord($data{$i}));
  169. }
  170. return $msg;
  171. }
  172. //用户加入
  173. function send($k,$msg){
  174. parse_str($msg,$g);
  175. $ar=array();
  176. if($g['type']=='add'){
  177. $this->users[$k]['name']=$g['ming'];
  178. $ar['type']='add';
  179. $ar['name']=$g['ming'];
  180. $key='all';
  181. }else{
  182. $ar['nrong']=$g['nr'];
  183. $key=$g['key'];
  184. }
  185. $this->send1($k,$ar,$key);
  186. }
  187. function getusers(){
  188. $ar=array();
  189. foreach($this->users as $k=>$v){
  190. $ar[]=array('code'=>$k,'name'=>$v['name']);
  191. }
  192. return $ar;
  193. }
  194. //$k 发信息人的code $key接受人的 code
  195. function send1($k,$ar,$key='all'){
  196. $ar['code1']=$key;
  197. $ar['code']=$k;
  198. $ar['time']=date('m-d H:i:s');
  199. $str = $this->code(json_encode($ar));
  200. if($key=='all'){
  201. $users=$this->users;
  202. if($ar['type']=='add'){
  203. $ar['type']='madd';
  204. $ar['users']=$this->getusers();
  205. $str1 = $this->code(json_encode($ar));
  206. socket_write($users[$k]['socket'],$str1,strlen($str1));
  207. unset($users[$k]);
  208. }
  209. foreach($users as $v){
  210. socket_write($v['socket'],$str,strlen($str));
  211. }
  212. }else{
  213. socket_write($this->users[$k]['socket'],$str,strlen($str));
  214. socket_write($this->users[$key]['socket'],$str,strlen($str));
  215. }
  216. }
  217. //用户退出
  218. function send2($k){
  219. $this->close($k);
  220. $ar['type']='rmove';
  221. $ar['nrong']=$k;
  222. $this->send1(false,$ar,'all');
  223. }
  224. function e($str){
  225. //$path=dirname(__FILE__).'/log.txt';
  226. $str=$str."\n";
  227. //error_log($str,3,$path);
  228. echo iconv('utf-8','gbk//IGNORE',$str);
  229. }
  230. }
  231. ?>
复制代码

很多童鞋反应用我的源码项目还是报错,不能运行,说下详细安装部署步骤。

首先把下载下来的源代码解压放到web目录下,比如我的就是applications/Xampp/xamppfiles/htdocs/phpb/websocket,

路径

路径

然后使用命令行工具cd进这个目录,运行命令:

  1. php websocket.php
复制代码

运行效果图:

命令行操作

接着打开Apache服务器,在浏览器访问http://localhost/phpb/websocket/client.html

运行效果图:

源码链接:webSoket-php-chatsever_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。



回复

使用道具 举报