首页 > 开发 > PHP > 正文

php+html5基于websocket实现聊天室的方法

2024-05-04 23:37:52
字体:
来源:转载
供稿:网友

这篇文章主要介绍了php+html5基于websocket实现聊天室的方法,实例分析了php结合html5的websocket通讯的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php+html5基于websocket实现聊天室的方法。分享给大家供大家参考。具体如下:

html5的websocket 实现了双向通信,折腾了几天弄了个聊天室,分享给大家

 

 
  1. <?php 
  2. error_reporting(E_ALL); 
  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. public function __construct($address, $port){ 
  11. $this->master=$this->WebSocket($address, $port); 
  12. $this->sockets=array('s'=>$this->master); 
  13. function run(){ 
  14. while(true){ 
  15. $changes=$this->sockets; 
  16. socket_select($changes,$write=NULL,$except=NULL,NULL); 
  17. foreach($changes as $sock){ 
  18. if($sock==$this->master){ 
  19. $client=socket_accept($this->master); 
  20. //$key=uniqid(); 
  21. $this->sockets[]=$client; 
  22. $this->users[]=array( 
  23. 'socket'=>$client, 
  24. 'shou'=>false 
  25. ); 
  26. }else
  27. $len=socket_recv($sock,$buffer,2048,0); 
  28. $k=$this->search($sock); 
  29. if($len<7){ 
  30. $name=$this->users[$k]['ming']; 
  31. $this->close($sock); 
  32. $this->send2($name,$k); 
  33. continue
  34. if(!$this->users[$k]['shou']){ 
  35. $this->woshou($k,$buffer); 
  36. }else
  37. $buffer = $this->uncode($buffer); 
  38. $this->send($k,$buffer); 
  39. function close($sock){ 
  40. $k=array_search($sock, $this->sockets); 
  41. socket_close($sock); 
  42. unset($this->sockets[$k]); 
  43. unset($this->users[$k]); 
  44. $this->e("key:$k close"); 
  45. function search($sock){ 
  46. foreach ($this->users as $k=>$v){ 
  47. if($sock==$v['socket']) 
  48. return $k; 
  49. return false
  50. function WebSocket($address,$port){ 
  51. $server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
  52. socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1); 
  53. socket_bind($server, $address, $port); 
  54. socket_listen($server); 
  55. $this->e('Server Started : '.date('Y-m-d H:i:s')); 
  56. $this->e('Listening on : '.$address.' port '.$port); 
  57. return $server; 
  58. function woshou($k,$buffer){ 
  59. $buf = substr($buffer,strpos($buffer,'Sec-WebSocket-Key:')+18); 
  60. $key = trim(substr($buf,0,strpos($buf,"/r/n"))); 
  61. $new_key = base64_encode(sha1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11",true)); 
  62. $new_message = "HTTP/1.1 101 Switching Protocols/r/n"
  63. $new_message .= "Upgrade: websocket/r/n"
  64. $new_message .= "Sec-WebSocket-Version: 13/r/n"
  65. $new_message .= "Connection: Upgrade/r/n"
  66. $new_message .= "Sec-WebSocket-Accept: " . $new_key . "/r/n/r/n"
  67. socket_write($this->users[$k]['socket'],$new_message,strlen($new_message)); 
  68. $this->users[$k]['shou']=true
  69. return true
  70. function uncode($str){ 
  71. $mask = array();  
  72. $data = '';  
  73. $msg = unpack('H*',$str);  
  74. $head = substr($msg[1],0,2);  
  75. if (hexdec($head{1}) === 8) {  
  76. $data = false;  
  77. }else if (hexdec($head{1}) === 1){  
  78. $mask[] = hexdec(substr($msg[1],4,2));  
  79. $mask[] = hexdec(substr($msg[1],6,2));  
  80. $mask[] = hexdec(substr($msg[1],8,2));  
  81. $mask[] = hexdec(substr($msg[1],10,2));  
  82. $s = 12;  
  83. $e = strlen($msg[1])-2;  
  84. $n = 0;  
  85. for ($i=$s; $i<= $e; $i+= 2) {  
  86. $data .= chr($mask[$n%4]^hexdec(substr($msg[1],$i,2))); 
  87. $n++;  
  88. }  
  89. }  
  90. return $data; 
  91. function code($msg){ 
  92. $msg = preg_replace(array('//r$/','//n$/','//r/n$/',), '', $msg); 
  93. $frame = array();  
  94. $frame[0] = '81';  
  95. $len = strlen($msg);  
  96. $frame[1] = $len<16?'0'.dechex($len):dechex($len);  
  97. $frame[2] = $this->ord_hex($msg);  
  98. $data = implode('',$frame);  
  99. return pack("H*", $data);  
  100. function ord_hex($data) {  
  101. $msg = '';  
  102. $l = strlen($data);  
  103. for ($i= 0; $i<$l; $i++) {  
  104. $msg .= dechex(ord($data{$i}));  
  105. }  
  106. return $msg;  
  107. function send($k,$msg){ 
  108. /*$this->send1($k,$this->code($msg),'all');*/ 
  109. parse_str($msg,$g); 
  110. $this->e($msg); 
  111. $ar=array(); 
  112. if($g['type']=='add'){ 
  113. $this->users[$k]['ming']=$g['ming']; 
  114. $ar['add']=true
  115. $ar['nrong']='欢迎'.$g['ming'].'加入!'
  116. $ar['users']=$this->getusers(); 
  117. $key='all'
  118. }else if($g['type']=='ltiao'){ 
  119. $ar['nrong']=$g['nr']; 
  120. $key=$g['key']; 
  121. $msg=json_encode($ar); 
  122. $this->e($msg); 
  123. $msg = $this->code($msg); 
  124. $this->send1($k,$msg,$key); 
  125. //socket_write($this->users[$k]['socket'],$msg,strlen($msg)); 
  126. function getusers(){ 
  127. $ar=array(); 
  128. foreach($this->users as $k=>$v){ 
  129. $ar[$k]=$v['ming']; 
  130. return $ar; 
  131. function send1($k,$str,$key='all'){ 
  132. if($key=='all'){ 
  133. foreach($this->users as $v){ 
  134. socket_write($v['socket'],$str,strlen($str)); 
  135. }else
  136. if($k!=$key) 
  137. socket_write($this->users[$k]['socket'],$str,strlen($str)); 
  138. socket_write($this->users[$key]['socket'],$str,strlen($str)); 
  139. function send2($ming,$k){ 
  140. $ar['remove']=true
  141. $ar['removekey']=$k; 
  142. $ar['nrong']=$ming.'退出聊天室'
  143. $str = $this->code(json_encode($ar)); 
  144. $this->send1(false,$str,'all'); 
  145. function e($str){ 
  146. $path=dirname(__FILE__).'/log.txt'
  147. $str=$str."/n"
  148. error_log($str,3,$path); 
  149. echo iconv('utf-8','gbk//IGNORE',$str); 
  150. ?> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表