php+webSoket实现聊天室示例代码(附源码)
程序员文章站
2024-03-07 16:24:21
最近在公司利用直播间搭建一个图文直播间时正好要用到chatsever,研究了一下html5的websocket 实现了双向通信,根据前人的经验折腾了几天弄了个聊天室,实现了...
最近在公司利用直播间搭建一个图文直播间时正好要用到chatsever,研究了一下html5的websocket 实现了双向通信,根据前人的经验折腾了几天弄了个聊天室,实现了发送图片,发送qq表情,群聊私聊等功能,特地分享给各位新手参考学习,大牛可以忽略。
前端:client.html
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/> <title>html5 websocket 网页聊天室 javascript php</title> <style type="text/css"> body,p{margin:0px; padding:0px; font-size:14px; color:#333; font-family:arial, helvetica, sans-serif;} #ltian,.rin{width:98%; margin:5px auto;} #ltian{border:1px #ccc solid;overflow-y:auto; overflow-x:hidden; position:relative;} #ct{margin-right:111px; height:100%;overflow-y:auto;overflow-x: hidden;} #us{width:110px; overflow-y:auto; overflow-x:hidden; float:right; border-left:1px #ccc solid; height:100%; background-color:#f1f1f1;} #us p{padding:3px 5px; color:#08c; line-height:20px; height:20px; cursor:pointer; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;} #us p:hover,#us p:active,#us p.ck{background-color:#069; color:#fff;} #us p.my:hover,#us p.my:active,#us p.my{color:#333;background-color:transparent;} button{float:right; width:80px; height:35px; font-size:18px;} input{width:100%; height:30px; padding:2px; line-height:20px; outline:none; border:solid 1px #ccc;} .rin p{margin-right:160px;} .rin span{float:right; padding:6px 5px 0px 5px; position:relative;} .rin span img{margin:0px 3px; cursor:pointer;} .rin span form{position:absolute; width:25px; height:25px; overflow:hidden; opacity:0; top:5px; right:5px;} .rin span input{width:180px; height:25px; margin-left:-160px; cursor:pointer} #ct p{padding:5px; line-height:20px;} #ct a{color:#069; cursor:pointer;} #ct span{color:#999; margin-right:10px;} .c2{color:#999;} .c3{background-color:#dbe9ec; padding:5px;} .qp{position:absolute; font-size:12px; color:#666; top:5px; right:130px; text-decoration:none; color:#069;} #ems{position:absolute; z-index:5; display:none; top:0px; left:0px; max-width:230px; background-color:#f1f1f1; border:solid 1px #ccc; padding:5px;} #ems img{width:44px; height:44px; border:solid 1px #fff; cursor:pointer;} #ems img:hover,#ems img:active{border-color:#a4b7e3;} #ems a{color:#069; border-radius:2px; display:inline-block; margin:2px 5px; padding:1px 8px; text-decoration:none; background-color:#d5dffd;} #ems a:hover,#ems a:active,#ems a.ck{color:#fff; background-color:#069;} .tc{text-align:center; margin-top:5px;} </style> </head> <body> <div id="ltian"> <div id="us" class="jb"></div> <div id="ct"></div> <a href="javascript:;" rel="external nofollow" rel="external nofollow" class="qp" onclick="this.parentnode.children[1].innerhtml=''">清屏</a> </div> <div class="rin"> <button id="sd">发送</button> <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> <p><input id="nrong"></p> </div> <div id="ems"><p></p><p class="tc"></p></div> <script> if(typeof(websocket)=='undefined'){ alert('你的浏览器不支持 websocket ,推荐使用google chrome 或者 mozilla firefox'); } </script> <script src="http://www.yxsss.com/ui/p/a.js" type="text/javascript"></script> <script> (function(){ var key='all',mkey; var users={}; var url='ws://127.0.0.1:8000'; var so=false,n=false; var lus=a.$('us'),lct=a.$('ct'); function st(){ var arr1 = ["聪明的","狡猾的","可爱的","美丽的","狡猾的","善良的","帅气的","逗比的"]; var arr2 = ["大灰狼","小白兔","母老虎","外星人","皮卡丘","hellokitty","吴亦凡","薛之谦"]; var ran1 = math.floor(math.random() * arr1.length + 1)-1; var ran2 = math.floor(math.random() * arr2.length + 1)-1; var n=arr1[ran1]+arr2[ran2]; //以上五行是用来随机生成用户昵称的方法,参考一下 ,如果想自定义用户名可以将以上五行注释,然后以下两行取消注释 //n=prompt('请给自己取一个霸气的名字:'); //n=n.substr(0,16); //console.log(n); if(!n){ return ; } so=new websocket(url); so.onopen=function(){ if(so.readystate==1){ so.send('type=add&ming='+n); } } so.onclose=function(){ so=false; lct.appendchild(a.$$('<p class="c2">退出聊天室</p>')); } so.onmessage=function(msg){ eval('var da='+msg.data); var obj=false,c=false; if(da.type=='add'){ var obj=a.$$('<p>'+da.name+'</p>'); lus.appendchild(obj); cuser(obj,da.code); obj=a.$$('<p><span>['+da.time+']</span>欢迎<a>'+da.name+'</a>加入</p>'); c=da.code; }else if(da.type=='madd'){ mkey=da.code; da.users.unshift({'code':'all','name':'大家'}); for(var i=0;i<da.users.length;i++){ var obj=a.$$('<p>'+da.users[i].name+'</p>'); lus.appendchild(obj); if(mkey!=da.users[i].code){ cuser(obj,da.users[i].code); }else{ obj.classname='my'; document.title=da.users[i].name; } } obj=a.$$('<p><span>['+da.time+']</span>欢迎'+da.name+'加入</p>'); users.all.classname='ck'; } if(obj==false){ if(da.type=='rmove'){ var obj=a.$$('<p class="c2"><span>['+da.time+']</span>'+users[da.nrong].innerhtml+'退出聊天室</p>'); lct.appendchild(obj); users[da.nrong].del(); delete users[da.nrong]; }else{ da.nrong=da.nrong.replace(/{\\(\d+)}/g,function(a,b){ return '<img src="sk/'+b+'.jpg">'; }).replace(/^data\:image\/png;base64\,.{50,}$/i,function(a){ return '<img src="'+a+'">'; }); //da.code 发信息人的code if(da.code1==mkey){ obj=a.$$('<p class="c3"><span>['+da.time+']</span><a>'+users[da.code].innerhtml+'</a>对我说:'+da.nrong+'</p>'); c=da.code; }else if(da.code==mkey){ if(da.code1!='all') obj=a.$$('<p class="c3"><span>['+da.time+']</span>我对<a>'+users[da.code1].innerhtml+'</a>说:'+da.nrong+'</p>'); else obj=a.$$('<p><span>['+da.time+']</span>我对<a>'+users[da.code1].innerhtml+'</a>说:'+da.nrong+'</p>'); c=da.code1; }else if(da.code==false){ obj=a.$$('<p><span>['+da.time+']</span>'+da.nrong+'</p>'); }else if(da.code1){ obj=a.$$('<p><span>['+da.time+']</span><a>'+users[da.code].innerhtml+'</a>对'+users[da.code1].innerhtml+'说:'+da.nrong+'</p>'); c=da.code; } } } if(c){ obj.children[1].onclick=function(){ users[c].onclick(); } } lct.appendchild(obj); lct.scrolltop=math.max(0,lct.scrollheight-lct.offsetheight); } } a.$('sd').onclick=function(){ if(!so){ return st(); } var da=a.$('nrong').value.trim(); if(da==''){ alert('内容不能为空'); return false; } a.$('nrong').value=''; so.send('nr='+esc(da)+'&key='+key); } a.$('nrong').onkeydown=function(e){ var e=e||event; if(e.keycode==13){ a.$('sd').onclick(); } } function esc(da){ da=da.replace(/</g,'<').replace(/>/g,'>').replace(/\"/g,'"'); return encodeuricomponent(da); } function cuser(t,code){ users[code]=t; t.onclick=function(){ t.parentnode.children.rcss('ck',''); t.rcss('','ck'); key=code; } } a.$('ltian').style.height=(document.documentelement.clientheight - 70)+'px'; st(); var bq=a.$('imgbq'),ems=a.$('ems'); var l=80,r=4,c=5,s=0,p=math.ceil(l/(r*c)); var pt='sk/'; bq.onclick=function(e){ var e=e||event; if(!so){ return st(); } ems.style.display='block'; document.onclick=function(){ gb(); } ct(); try{e.stoppropagation();}catch(o){} } for(var i=0;i<p;i++){ var a=a.$$('<a href="javascript:;" rel="external nofollow" rel="external nofollow" >'+(i+1)+'</a>'); ems.children[1].appendchild(a); ef(a,i); } ems.children[1].children[0].classname='ck'; function ct(){ var wz=bq.weiz(); with(ems.style){ top=wz.y-242+'px'; left=wz.x+bq.offsetwidth-235+'px'; } } function ef(t,i){ t.onclick=function(e){ var e=e||event; s=i*r*c; ems.children[0].innerhtml=''; hh(); this.parentnode.children.rcss('ck',''); this.rcss('','ck'); try{e.stoppropagation();}catch(o){} } } function hh(){ var z=math.min(l,s+r*c); for(var i=s;i<z;i++){ var a=a.$$('<img src="'+pt+i+'.jpg">'); hh1(a,i); ems.children[0].appendchild(a); } ct(); } function hh1(t,i){ t.onclick=function(e){ var e=e||event; a.$('nrong').value+='{\\'+i+'}'; if(!e.ctrlkey){ gb(); } try{e.stoppropagation();}catch(o){} } } function gb(){ ems.style.display=''; a.$('nrong').focus(); document.onclick=''; } hh(); a.on(window,'resize',function(){ a.$('ltian').style.height=(document.documentelement.clientheight - 70)+'px'; ct(); }) var fimg=a.$('upimg'); var img=new image(); var dw=400,dh=300; a.on(fimg,'change',function(ev){ if(!so){ st(); return false; } if(key=='all'){ alert('由于资源限制 发图只能私聊'); return false; } var f=ev.target.files[0]; if(f.type.match('image.*')){ var r = new filereader(); r.onload = function(e){ img.setattribute('src',e.target.result); }; r.readasdataurl(f); } }); img.onload=function(){ ih=img.height,iw=img.width; if(iw/ih > dw/dh && iw > dw){ ih=ih/iw*dw; iw=dw; }else if(ih > dh){ iw=iw/ih*dh; ih=dh; } var rc = a.$$('canvas'); var ct = rc.getcontext('2d'); rc.width=iw; rc.height=ih; ct.drawimage(img,0,0,iw,ih); var da=rc.todataurl(); so.send('nr='+esc(da)+'&key='+key); } })(); </script> </body> </html>
后端代码:webserver.php
<?php error_reporting(e_all ^ e_notice); ob_implicit_flush(); $sk=new sock('127.0.0.1',8000); $sk->run(); class sock{ public $sockets; public $users; public $master; private $sda=array();//已接收的数据 private $slen=array();//数据总长度 private $sjen=array();//接收数据的长度 private $ar=array();//加密key private $n=array(); public function __construct($address, $port){ $this->master=$this->websocket($address, $port); $this->sockets=array($this->master); } function run(){ while(true){ $changes=$this->sockets; $write=null; $except=null; socket_select($changes,$write,$except,null); foreach($changes as $sock){ if($sock==$this->master){ $client=socket_accept($this->master); $key=uniqid(); $this->sockets[]=$client; $this->users[$key]=array( 'socket'=>$client, 'shou'=>false ); }else{ $len=0; $buffer=''; do{ $l=socket_recv($sock,$buf,1000,0); $len+=$l; $buffer.=$buf; }while($l==1000); $k=$this->search($sock); if($len<7){ $this->send2($k); continue; } if(!$this->users[$k]['shou']){ $this->woshou($k,$buffer); }else{ $buffer = $this->uncode($buffer,$k); if($buffer==false){ continue; } $this->send($k,$buffer); } } } } } function close($k){ socket_close($this->users[$k]['socket']); unset($this->users[$k]); $this->sockets=array($this->master); foreach($this->users as $v){ $this->sockets[]=$v['socket']; } $this->e("key:$k close"); } function search($sock){ foreach ($this->users as $k=>$v){ if($sock==$v['socket']) return $k; } return false; } function websocket($address,$port){ $server = socket_create(af_inet, sock_stream, sol_tcp); socket_set_option($server, sol_socket, so_reuseaddr, 1); socket_bind($server, $address, $port); socket_listen($server); $this->e('server started : '.date('y-m-d h:i:s')); $this->e('listening on : '.$address.' port '.$port); return $server; } function woshou($k,$buffer){ $buf = substr($buffer,strpos($buffer,'sec-websocket-key:')+18); $key = trim(substr($buf,0,strpos($buf,"\r\n"))); $new_key = base64_encode(sha1($key."258eafa5-e914-47da-95ca-c5ab0dc85b11",true)); $new_message = "http/1.1 101 switching protocols\r\n"; $new_message .= "upgrade: websocket\r\n"; $new_message .= "sec-websocket-version: 13\r\n"; $new_message .= "connection: upgrade\r\n"; $new_message .= "sec-websocket-accept: " . $new_key . "\r\n\r\n"; socket_write($this->users[$k]['socket'],$new_message,strlen($new_message)); $this->users[$k]['shou']=true; return true; } function uncode($str,$key){ $mask = array(); $data = ''; $msg = unpack('h*',$str); $head = substr($msg[1],0,2); if ($head == '81' && !isset($this->slen[$key])) { $len=substr($msg[1],2,2); $len=hexdec($len); if(substr($msg[1],2,2)=='fe'){ $len=substr($msg[1],4,4); $len=hexdec($len); $msg[1]=substr($msg[1],4); }else if(substr($msg[1],2,2)=='ff'){ $len=substr($msg[1],4,16); $len=hexdec($len); $msg[1]=substr($msg[1],16); } $mask[] = hexdec(substr($msg[1],4,2)); $mask[] = hexdec(substr($msg[1],6,2)); $mask[] = hexdec(substr($msg[1],8,2)); $mask[] = hexdec(substr($msg[1],10,2)); $s = 12; $n=0; }else if($this->slen[$key] > 0){ $len=$this->slen[$key]; $mask=$this->ar[$key]; $n=$this->n[$key]; $s = 0; } $e = strlen($msg[1])-2; for ($i=$s; $i<= $e; $i+= 2) { $data .= chr($mask[$n%4]^hexdec(substr($msg[1],$i,2))); $n++; } $dlen=strlen($data); if($len > 255 && $len > $dlen+intval($this->sjen[$key])){ $this->ar[$key]=$mask; $this->slen[$key]=$len; $this->sjen[$key]=$dlen+intval($this->sjen[$key]); $this->sda[$key]=$this->sda[$key].$data; $this->n[$key]=$n; return false; }else{ unset($this->ar[$key],$this->slen[$key],$this->sjen[$key],$this->n[$key]); $data=$this->sda[$key].$data; unset($this->sda[$key]); return $data; } } function code($msg){ $frame = array(); $frame[0] = '81'; $len = strlen($msg); if($len < 126){ $frame[1] = $len<16?'0'.dechex($len):dechex($len); }else if($len < 65025){ $s=dechex($len); $frame[1]='7e'.str_repeat('0',4-strlen($s)).$s; }else{ $s=dechex($len); $frame[1]='7f'.str_repeat('0',16-strlen($s)).$s; } $frame[2] = $this->ord_hex($msg); $data = implode('',$frame); return pack("h*", $data); } function ord_hex($data) { $msg = ''; $l = strlen($data); for ($i= 0; $i<$l; $i++) { $msg .= dechex(ord($data{$i})); } return $msg; } //用户加入 function send($k,$msg){ parse_str($msg,$g); $ar=array(); if($g['type']=='add'){ $this->users[$k]['name']=$g['ming']; $ar['type']='add'; $ar['name']=$g['ming']; $key='all'; }else{ $ar['nrong']=$g['nr']; $key=$g['key']; } $this->send1($k,$ar,$key); } function getusers(){ $ar=array(); foreach($this->users as $k=>$v){ $ar[]=array('code'=>$k,'name'=>$v['name']); } return $ar; } //$k 发信息人的code $key接受人的 code function send1($k,$ar,$key='all'){ $ar['code1']=$key; $ar['code']=$k; $ar['time']=date('m-d h:i:s'); $str = $this->code(json_encode($ar)); if($key=='all'){ $users=$this->users; if($ar['type']=='add'){ $ar['type']='madd'; $ar['users']=$this->getusers(); $str1 = $this->code(json_encode($ar)); socket_write($users[$k]['socket'],$str1,strlen($str1)); unset($users[$k]); } foreach($users as $v){ socket_write($v['socket'],$str,strlen($str)); } }else{ socket_write($this->users[$k]['socket'],$str,strlen($str)); socket_write($this->users[$key]['socket'],$str,strlen($str)); } } //用户退出 function send2($k){ $this->close($k); $ar['type']='rmove'; $ar['nrong']=$k; $this->send1(false,$ar,'all'); } function e($str){ //$path=dirname(__file__).'/log.txt'; $str=$str."\n"; //error_log($str,3,$path); echo iconv('utf-8','gbk//ignore',$str); } } ?>
很多童鞋反应用我的源码项目还是报错,不能运行,说下详细安装部署步骤。
首先把下载下来的源代码解压放到web目录下,比如我的就是applications/xampp/xamppfiles/htdocs/phpb/websocket,
路径
然后使用命令行工具cd进这个目录,运行命令:
php websocket.php
运行效果图:
命令行操作
接着打开apache服务器,在浏览器访问http://localhost/phpb/websocket/client.html
运行效果图:
源码链接:websoket-php-chatsever_jb51.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
php+webSoket实现聊天室示例代码(附源码)
-
ASP.NET中repeater嵌套实现代码(附源码)
-
ASP.NET中repeater嵌套实现代码(附源码)
-
Spring+SpringMVC+JDBC实现登录的示例(附源码)
-
Android编程实现简易弹幕效果示例【附demo源码下载】
-
Java实现的打地鼠小游戏完整示例【附源码下载】
-
Android编程实现仿美团或淘宝的多级分类菜单效果示例【附demo源码下载】
-
Android编程基于自定义view实现公章效果示例【附源码下载】
-
Android开发实现Switch控件修改样式功能示例【附源码下载】
-
原生PHP实现导出csv格式Excel文件的方法示例【附源码下载】