php+html5基于websocket实现聊天室的方法
本文实例讲述了php+html5基于websocket实现聊天室的方法。分享给大家供大家参考。具体如下:
html5的websocket实现了双向通信,折腾了几天弄了个聊天室,分享给大家
<?php error_reporting(E_ALL); ob_implicit_flush(); $sk=newSock('127.0.0.1',8000); $sk->run(); classSock{ public$sockets; public$users; public$master; publicfunction__construct($address,$port){ $this->master=$this->WebSocket($address,$port); $this->sockets=array('s'=>$this->master); } functionrun(){ while(true){ $changes=$this->sockets; socket_select($changes,$write=NULL,$except=NULL,NULL); foreach($changesas$sock){ if($sock==$this->master){ $client=socket_accept($this->master); //$key=uniqid(); $this->sockets[]=$client; $this->users[]=array( 'socket'=>$client, 'shou'=>false ); }else{ $len=socket_recv($sock,$buffer,2048,0); $k=$this->search($sock); if($len<7){ $name=$this->users[$k]['ming']; $this->close($sock); $this->send2($name,$k); continue; } if(!$this->users[$k]['shou']){ $this->woshou($k,$buffer); }else{ $buffer=$this->uncode($buffer); $this->send($k,$buffer); } } } } } functionclose($sock){ $k=array_search($sock,$this->sockets); socket_close($sock); unset($this->sockets[$k]); unset($this->users[$k]); $this->e("key:$kclose"); } functionsearch($sock){ foreach($this->usersas$k=>$v){ if($sock==$v['socket']) return$k; } returnfalse; } functionWebSocket($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('ServerStarted:'.date('Y-m-dH:i:s')); $this->e('Listeningon:'.$address.'port'.$port); return$server; } functionwoshou($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.1101SwitchingProtocols\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; returntrue; } functionuncode($str){ $mask=array(); $data=''; $msg=unpack('H*',$str); $head=substr($msg[1],0,2); if(hexdec($head{1})===8){ $data=false; }elseif(hexdec($head{1})===1){ $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; $e=strlen($msg[1])-2; $n=0; for($i=$s;$i<=$e;$i+=2){ $data.=chr($mask[$n%4]^hexdec(substr($msg[1],$i,2))); $n++; } } return$data; } functioncode($msg){ $msg=preg_replace(array('/\r$/','/\n$/','/\r\n$/',),'',$msg); $frame=array(); $frame[0]='81'; $len=strlen($msg); $frame[1]=$len<16?'0'.dechex($len):dechex($len); $frame[2]=$this->ord_hex($msg); $data=implode('',$frame); returnpack("H*",$data); } functionord_hex($data){ $msg=''; $l=strlen($data); for($i=0;$i<$l;$i++){ $msg.=dechex(ord($data{$i})); } return$msg; } functionsend($k,$msg){ /*$this->send1($k,$this->code($msg),'all');*/ parse_str($msg,$g); $this->e($msg); $ar=array(); if($g['type']=='add'){ $this->users[$k]['ming']=$g['ming']; $ar['add']=true; $ar['nrong']='欢迎'.$g['ming'].'加入!'; $ar['users']=$this->getusers(); $key='all'; }elseif($g['type']=='ltiao'){ $ar['nrong']=$g['nr']; $key=$g['key']; } $msg=json_encode($ar); $this->e($msg); $msg=$this->code($msg); $this->send1($k,$msg,$key); //socket_write($this->users[$k]['socket'],$msg,strlen($msg)); } functiongetusers(){ $ar=array(); foreach($this->usersas$k=>$v){ $ar[$k]=$v['ming']; } return$ar; } functionsend1($k,$str,$key='all'){ if($key=='all'){ foreach($this->usersas$v){ socket_write($v['socket'],$str,strlen($str)); } }else{ if($k!=$key) socket_write($this->users[$k]['socket'],$str,strlen($str)); socket_write($this->users[$key]['socket'],$str,strlen($str)); } } functionsend2($ming,$k){ $ar['remove']=true; $ar['removekey']=$k; $ar['nrong']=$ming.'退出聊天室'; $str=$this->code(json_encode($ar)); $this->send1(false,$str,'all'); } functione($str){ $path=dirname(__FILE__).'/log.txt'; $str=$str."\n"; error_log($str,3,$path); echoiconv('utf-8','gbk//IGNORE',$str); } } ?>
希望本文所述对大家的php程序设计有所帮助。