php結合html5的websocket通訊的使用方法

墨辰丷
發布: 2023-03-30 21:08:02
原創
1830 人瀏覽過

這篇文章主要介紹php結合html5的websocket通訊的使用方法,有興趣的朋友參考下,希望對大家有幫助。

本文實例講述了php html5基於websocket實現聊天室的方法,具體如下:

html5的websocket 實現了雙向通信,折騰了幾天弄了個聊天室,分享給大家

<?php
error_reporting(E_ALL);
ob_implicit_flush();
$sk=new Sock(&#39;127.0.0.1&#39;,8000);
$sk->run();
class Sock{
 public $sockets;
 public $users;
 public $master;
 public function __construct($address, $port){
  $this->master=$this->WebSocket($address, $port);
  $this->sockets=array(&#39;s&#39;=>$this->master);
 }
 function run(){
  while(true){
   $changes=$this->sockets;
   socket_select($changes,$write=NULL,$except=NULL,NULL);
   foreach($changes as $sock){
    if($sock==$this->master){
     $client=socket_accept($this->master);
     //$key=uniqid();
     $this->sockets[]=$client;
     $this->users[]=array(
      &#39;socket&#39;=>$client,
      &#39;shou&#39;=>false
     );
    }else{
     $len=socket_recv($sock,$buffer,2048,0);
     $k=$this->search($sock);
     if($len<7){
      $name=$this->users[$k][&#39;ming&#39;];
      $this->close($sock);
      $this->send2($name,$k);
      continue;
     }
     if(!$this->users[$k][&#39;shou&#39;]){
      $this->woshou($k,$buffer);
     }else{
      $buffer = $this->uncode($buffer);
      $this->send($k,$buffer);
     }
    }
   }
  }
 }
 function close($sock){
  $k=array_search($sock, $this->sockets);
  socket_close($sock);
  unset($this->sockets[$k]);
  unset($this->users[$k]);
  $this->e("key:$k close");
 }
 function search($sock){
  foreach ($this->users as $k=>$v){
   if($sock==$v[&#39;socket&#39;])
   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(&#39;Server Started : &#39;.date(&#39;Y-m-d H:i:s&#39;));
  $this->e(&#39;Listening on : &#39;.$address.&#39; port &#39;.$port);
  return $server;
 }
 function woshou($k,$buffer){
  $buf = substr($buffer,strpos($buffer,&#39;Sec-WebSocket-Key:&#39;)+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][&#39;socket&#39;],$new_message,strlen($new_message));
  $this->users[$k][&#39;shou&#39;]=true;
  return true;
 }
 function uncode($str){
  $mask = array(); 
  $data = &#39;&#39;; 
  $msg = unpack(&#39;H*&#39;,$str); 
  $head = substr($msg[1],0,2); 
  if (hexdec($head{1}) === 8) { 
   $data = false; 
  }else if (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;
 }
 function code($msg){
  $msg = preg_replace(array(&#39;/\r$/&#39;,&#39;/\n$/&#39;,&#39;/\r\n$/&#39;,), &#39;&#39;, $msg);
  $frame = array(); 
  $frame[0] = &#39;81&#39;; 
  $len = strlen($msg); 
  $frame[1] = $len<16?&#39;0&#39;.dechex($len):dechex($len); 
  $frame[2] = $this->ord_hex($msg); 
  $data = implode(&#39;&#39;,$frame); 
  return pack("H*", $data); 
 }
 function ord_hex($data) { 
  $msg = &#39;&#39;; 
  $l = strlen($data); 
  for ($i= 0; $i<$l; $i++) { 
   $msg .= dechex(ord($data{$i})); 
  } 
  return $msg; 
 }
 function send($k,$msg){
  /*$this->send1($k,$this->code($msg),&#39;all&#39;);*/
  parse_str($msg,$g);
  $this->e($msg);
  $ar=array();
  if($g[&#39;type&#39;]==&#39;add&#39;){
   $this->users[$k][&#39;ming&#39;]=$g[&#39;ming&#39;];
   $ar[&#39;add&#39;]=true;
   $ar[&#39;nrong&#39;]=&#39;欢迎&#39;.$g[&#39;ming&#39;].&#39;加入!&#39;;
   $ar[&#39;users&#39;]=$this->getusers();
   $key=&#39;all&#39;;
  }else if($g[&#39;type&#39;]==&#39;ltiao&#39;){
   $ar[&#39;nrong&#39;]=$g[&#39;nr&#39;];
   $key=$g[&#39;key&#39;];
  }
  $msg=json_encode($ar);
  $this->e($msg);
  $msg = $this->code($msg);
  $this->send1($k,$msg,$key);
  //socket_write($this->users[$k][&#39;socket&#39;],$msg,strlen($msg));
 }
 function getusers(){
  $ar=array();
  foreach($this->users as $k=>$v){
   $ar[$k]=$v[&#39;ming&#39;];
  }
  return $ar;
 }
 function send1($k,$str,$key=&#39;all&#39;){
  if($key==&#39;all&#39;){
   foreach($this->users as $v){
    socket_write($v[&#39;socket&#39;],$str,strlen($str));
   }
  }else{
   if($k!=$key)
   socket_write($this->users[$k][&#39;socket&#39;],$str,strlen($str));
   socket_write($this->users[$key][&#39;socket&#39;],$str,strlen($str));
  }
 }
 function send2($ming,$k){
  $ar[&#39;remove&#39;]=true;
  $ar[&#39;removekey&#39;]=$k;
  $ar[&#39;nrong&#39;]=$ming.&#39;退出聊天室&#39;;
  $str = $this->code(json_encode($ar));
  $this->send1(false,$str,&#39;all&#39;);
 }
 function e($str){
  $path=dirname(__FILE__).&#39;/log.txt&#39;;
  $str=$str."\n";
  error_log($str,3,$path);
  echo iconv(&#39;utf-8&#39;,&#39;gbk//IGNORE&#39;,$str);
 }
}
?>
登入後複製

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

PHP彈出對話框

如何利用php自動執行.sql檔案

php中實作影片拍照

############################

以上是php結合html5的websocket通訊的使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板