透過websocket,實現遊戲陪玩系統的聊天室

雲豹科技曉彤發表於2021-09-06

聊天室是遊戲陪玩系統重要功能,透過聊天室,使用者之間可以更好的進行交流,實現遊戲陪玩系統的社交功能。

<?php
error_reporting(E_ALL);
ob_implicit_flush();
$sk=new Sock('127.0.0.1',8000);
$sk->run();
class Sock{
 public $sockets;
 public $users;
 public $master;
 public function __construct(address,address,port){
  this->master=this−>master=this->WebSocket(address,address,port);
  this->sockets=array('s'=>this−>sockets=array(′s′=>this->master);
 }
 function run(){
  while(true){
   changes=changes=this->sockets;
   socket_select(changes,changes,write=NULL,$except=NULL,NULL);
   foreach(changes aschangesassock){
    if(sock==sock==this->master){
     client=socket_accept(client=socketaccept(this->master);
     //$key=uniqid();
     this->sockets[]=this−>sockets[]=client;
     $this->users[]=array(
      'socket'=>$client,
      'shou'=>false
     );
    }else{
     len=socket_recv(len=socketrecv(sock,$buffer,2048,0);
     k=k=this->search($sock);
     if($len<7){
      name=name=this->users[$k]['ming'];
      this->close(this−>close(sock);
      this->send2(this−>send2(name,$k);
      continue;
     }
     if(!this->users[this−>users[k]['shou']){
      this->woshou(this−>woshou(k,$buffer);
     }else{
      buffer =buffer=this->uncode($buffer);
      this->send(this−>send(k,$buffer);
     }
    }
   }
  }
 }
 function close($sock){
  k=array_search(k=arraysearch(sock, $this->sockets);
  socket_close($sock);
  unset(this->sockets[this−>sockets[k]);
  unset(this->users[this−>users[k]);
  this->e("key:this−>e("key:k close");
 }
 function search($sock){
  foreach (this->users asthis−>usersask=>$v){
   if(sock==sock==v['socket'])
   return $k;
  }
  return false;
 }
 function WebSocket(address,address,port){
  $server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
  socket_bind(server,server,address, $port);
  socket_listen($server);
  $this->e('Server Started : '.date('Y-m-d H:i:s'));
  this->e('Listening on : '.this−>e(′Listeningon:′.address.' port '.$port);
  return $server;
 }
 function woshou(k,k,buffer){
  buf = substr(buf=substr(buffer,strpos($buffer,'Sec-WebSocket-Key:')+18);
  key = trim(substr(key=trim(substr(buf,0,strpos($buf,"\r\n")));
  new_key = base64_encode(sha1(newkey=base64encode(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: " .newmessage.="Sec−WebSocket−Accept:".new_key . "\r\n\r\n";
  socket_write(this->users[this−>users[k]['socket'],new_message,strlen(newmessage,strlen(new_message));
  this->users[this−>users[k]['shou']=true;
  return true;
 }
 function uncode($str){
  $mask = array(); 
  $data = ''; 
  msg = unpack('H*',msg=unpack(′H∗′,str); 
  head = substr(head=substr(msg[1],0,2); 
  if (hexdec($head{1}) === 8) { 
   $data = false; 
  }else if (hexdec($head{1}) === 1){ 
   mask[] = hexdec(substr(mask[]=hexdec(substr(msg[1],4,2)); 
   mask[] = hexdec(substr(mask[]=hexdec(substr(msg[1],6,2)); 
   mask[] = hexdec(substr(mask[]=hexdec(substr(msg[1],8,2)); 
   mask[] = hexdec(substr(mask[]=hexdec(substr(msg[1],10,2)); 
   $s = 12; 
   e = strlen(e=strlen(msg[1])-2; 
   $n = 0; 
   for (i=i=s; i<=i<=e; $i+= 2) { 
    data .= chr(data.=chr(mask[n%4]^hexdec(substr(nmsg[1],$i,2)));
    $n++; 
   } 
  } 
  return $data;
 }
 function code($msg){
  $msg = preg_replace(array('/\r$/','/\n$/','/\r\n$/',), '', $msg);
  $frame = array(); 
  $frame[0] = '81'; 
  len = strlen(len=strlen(msg); 
  frame[1] =frame[1]=len<16?'0'.dechex(len):dechex(len):dechex(len); 
  frame[2] =frame[2]=this->ord_hex($msg); 
  data = implode('',data=implode(′′,frame); 
  return pack("H*", $data); 
 }
 function ord_hex($data) { 
  $msg = ''; 
  l = strlen(l=strlen(data); 
  for (i= 0;i=0;i<l;l;i++) { 
   msg .= dechex(ord(msg.=dechex(ord(data{$i})); 
  } 
  return $msg; 
 }
 function send(k,k,msg){
  /*this->send1(this−>send1(k,this->code(this−>code(msg),'all');*/
  parse_str(msg,msg,g);
  this->e(this−>e(msg);
  $ar=array();
  if($g['type']=='add'){
   this->users[this−>users[k]['ming']=$g['ming'];
   $ar['add']=true;
   ar['nrong']='歡迎'.ar[′nrong′]=′歡迎′.g['ming'].'加入!';
   ar['users']=ar[′users′]=this->getusers();
   $key='all';
  }else if($g['type']=='ltiao'){
   ar['nrong']=ar[′nrong′]=g['nr'];
   key=key=g['key'];
  }
  msg=json_encode(msg=jsonencode(ar);
  this->e(this−>e(msg);
  msg =msg=this->code($msg);
  this->send1(this−>send1(k,msg,msg,key);
  //socket_write(this->users[this−>users[k]['socket'],msg,strlen(msg,strlen(msg));
 }
 function getusers(){
  $ar=array();
  foreach(this->users asthis−>usersask=>$v){
   ar[ar[k]=$v['ming'];
  }
  return $ar;
 }
 function send1(k,k,str,$key='all'){
  if($key=='all'){
   foreach(this->users asthis−>usersasv){
    socket_write(v['socket'],v[′socket′],str,strlen($str));
   }
  }else{
   if(k!=k!=key)
   socket_write(this->users[this−>users[k]['socket'],str,strlen(str,strlen(str));
   socket_write(this->users[this−>users[key]['socket'],str,strlen(str,strlen(str));
  }
 }
 function send2(ming,ming,k){
  $ar['remove']=true;
  ar['removekey']=ar[′removekey′]=k;
  ar['nrong']=ar[′nrong′]=ming.'退出聊天室';
  str =str=this->code(json_encode($ar));
  this->send1(false,this−>send1(false,str,'all');
 }
 function e($str){
  $path=dirname(__FILE__).'/log.txt';
  str=str=str."\n";
  error_log(str,3,str,3,path);
  echo iconv('utf-8','gbk//IGNORE',$str);
 }
}
?>

這樣,一個簡單的遊戲陪玩系統聊天室功能就實現了。

宣告:本文由雲豹科技轉發自蝸牛奔跑部落格,如有侵權請聯絡作者刪除


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70002045/viewspace-2790642/,如需轉載,請註明出處,否則將追究法律責任。

相關文章