亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > PHP > 正文

php socket實現的聊天室代碼分享

2024-05-04 23:24:50
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了php socket實現的聊天室代碼分享,本文實現代碼來自國外友人,需要的朋友可以參考下
 
 
/*** patServer* PHP socket server base class* Events that can be handled:*  * onStart*  * onConnect*  * onConnectionRefused*  * onClose*  * onShutdown*  * onReceiveData** @version 1.1* @author  Stephan Schmidt <schst@php-tools.de>* @package patServer*/class patServer {/*** information about the project* @var array $systemVars*/var $systemVars  = array(  "appName"  => "patServer",  "appVersion"  => "1.1",  "author"  => array("Stephan Schmidt <schst@php-tools.de>", )  );/*** port to listen* @var integer  $port*/  var $port  = 10000;/*** domain to bind to* @var string $domain*/  var $domain  = "localhost";/*** maximum amount of clients* @var integer $maxClients*/  var $maxClients = -1;/*** buffer size for socket_read* @var integer $readBufferSize*/  var $readBufferSize  = 128;/*** end character for socket_read* @var integer $readEndCharacter*/  var $readEndCharacter = "/n";/*** maximum of backlog in queue* @var integer $maxQueue*/  var $maxQueue = 500;/*** debug mode* @var boolean $debug*/  var $debug  = true;/*** debug mode* @var string $debugMode*/  var $debugMode = "text";/*** debug destination (filename or stdout)* @var string $debugDest*/  var $debugDest = "stdout";/*** empty array, used for socket_select* @var array $null*/  var $null  = array();/*** all file descriptors are stored here* @var array $clientFD*/  var $clientFD = array();/*** needed to store client information* @var array $clientInfo*/  var $clientInfo = array();/*** needed to store server information* @var array $serverInfo*/  var $serverInfo = array();/*** amount of clients* @var integer  $clients*/  var $clients = 0;/*** create a new socket server** @access public* @param string  $domain  domain to bind to* @param integer  $port  port to listen to*/function patServer( $domain = "localhost", $port = 10000 ){  $this->domain = $domain;  $this->port  = $port;  $this->serverInfo["domain"]     = $domain;  $this->serverInfo["port"]     = $port;  $this->serverInfo["servername"]   = $this->systemVars["appName"];  $this->serverInfo["serverversion"] = $this->systemVars["appVersion"];  set_time_limit( 0 );}/*** set maximum amount of simultaneous connections** @access public* @param int $maxClients*/function setMaxClients( $maxClients ){  $this->maxClients = $maxClients;}/*** set debug mode** @access public* @param mixed $debug [text|htmlfalse]* @param string $dest destination of debug message (stdout to output or filename if log should be written)*/function setDebugMode( $debug, $dest = "stdout" ){  if( $debug === false )  {  $this->debug = false;  return true;  }  $this->debug  = true;  $this->debugMode = $debug;  $this->debugDest = $dest;}/*** start the server** @access public* @param int $maxClients*/function start(){  $this->initFD = @socket_create( AF_INET, SOCK_STREAM, 0 );  if( !$this->initFD )  die( "patServer: Could not create socket." );  // adress may be reused  socket_setopt( $this->initFD, SOL_SOCKET, SO_REUSEADDR, 1 );  // bind the socket  if( !@socket_bind( $this->initFD, $this->domain, $this->port ) )  {  @socket_close( $this->initFD );  die( "patServer: Could not bind socket to ".$this->domain." on port ".$this->port." ( ".$this->getLastSocketError( $this->initFd )." )." );  }  // listen on selected port  if( !@socket_listen( $this->initFD, $this->maxQueue ) )  die( "patServer: Could not listen ( ".$this->getLastSocketError( $this->initFd )." )." );  $this->sendDebugMessage( "Listening on port ".$this->port.". Server started at ".date( "H:i:s", time() ) );  // this allows the shutdown function to check whether the server is already shut down  $GLOBALS["_patServerStatus"] = "running";  // this ensures that the server will be sutdown correctly  register_shutdown_function( array( $this, "shutdown" ) );  if( method_exists( $this, "onStart" ) )  $this->onStart();  $this->serverInfo["started"] = time();  $this->serverInfo["status"]  = "running";  while( true )  {  $readFDs = array();  array_push( $readFDs, $this->initFD );  // fetch all clients that are awaiting connections  for( $i = 0; $i < count( $this->clientFD ); $i++ )   if( isset( $this->clientFD[$i] ) )   array_push( $readFDs, $this->clientFD[$i] );  // block and wait for data or new connection  $ready = @socket_select( $readFDs, $this->null, $this->null, NULL );  if( $ready === false )  {   $this->sendDebugMessage( "socket_select failed." );   $this->shutdown();  }  // check for new connection  if( in_array( $this->initFD, $readFDs ) )  {   $newClient = $this->acceptConnection( $this->initFD );   // check for maximum amount of connections   if( $this->maxClients > 0 )   {   if( $this->clients > $this->maxClients )   {    $this->sendDebugMessage( "Too many connections." );    if( method_exists( $this, "onConnectionRefused" ) )    $this->onConnectionRefused( $newClient );    $this->closeConnection( $newClient );   }   }   if( --$ready <= 0 )   continue;  }  // check all clients for incoming data  for( $i = 0; $i < count( $this->clientFD ); $i++ )  {   if( !isset( $this->clientFD[$i] ) )   continue;   if( in_array( $this->clientFD[$i], $readFDs ) )   {   $data = $this->readFromSocket( $i );   // empty data => connection was closed   if( !$data )   {    $this->sendDebugMessage( "Connection closed by peer" );    $this->closeConnection( $i );   }   else   {    $this->sendDebugMessage( "Received ".trim( $data )." from ".$i );    if( method_exists( $this, "onReceiveData" ) )    $this->onReceiveData( $i, $data );   }   }  }  }}/*** read from a socket** @access private* @param integer $clientId internal id of the client to read from* @return string $data  data that was read*/function readFromSocket( $clientId ){  // start with empty string  $data  = "";  // read data from socket  while( $buf = socket_read( $this->clientFD[$clientId], $this->readBufferSize ) )  {  $data .= $buf;  $endString = substr( $buf, - strlen( $this->readEndCharacter ) );  if( $endString == $this->readEndCharacter )   break;  if( $buf == NULL )   break;  }  if( $buf === false )  $this->sendDebugMessage( "Could not read from client ".$clientId." ( ".$this->getLastSocketError( $this->clientFD[$clientId] )." )." );  return $data;}/*** accept a new connection** @access public* @param resource &$socket socket that received the new connection* @return int  $clientID internal ID of the client*/function acceptConnection( &$socket ){  for( $i = 0 ; $i <= count( $this->clientFD ); $i++ )  {  if( !isset( $this->clientFD[$i] ) || $this->clientFD[$i] == NULL )  {   $this->clientFD[$i] = socket_accept( $socket );   socket_setopt( $this->clientFD[$i], SOL_SOCKET, SO_REUSEADDR, 1 );   $peer_host = "";   $peer_port = "";   socket_getpeername( $this->clientFD[$i], $peer_host, $peer_port );   $this->clientInfo[$i] = array(       "host"  => $peer_host,       "port"  => $peer_port,       "connectOn" => time()       );   $this->clients++;   $this->sendDebugMessage( "New connection ( ".$i." ) from ".$peer_host." on port ".$peer_port );   if( method_exists( $this, "onConnect" ) )   $this->onConnect( $i );   return $i;  }  }}/*** check, whether a client is still connected** @access public* @param integer $id client id* @return boolean $connected true if client is connected, false otherwise*/function isConnected( $id ){  if( !isset( $this->clientFD[$id] ) )  return false;  return true;}/*** close connection to a client** @access public* @param int $clientID internal ID of the client*/function closeConnection( $id ){  if( !isset( $this->clientFD[$id] ) )  return false;  if( method_exists( $this, "onClose" ) )  $this->onClose( $id );  $this->sendDebugMessage( "Closed connection ( ".$id." ) from ".$this->clientInfo[$id]["host"]." on port ".$this->clientInfo[$id]["port"] );  @socket_close( $this->clientFD[$id] );  $this->clientFD[$id] = NULL;  unset( $this->clientInfo[$id] );  $this->clients--;}/*** shutdown server** @access public*/function shutDown(){  if( $GLOBALS["_patServerStatus"] != "running" )  exit;  $GLOBALS["_patServerStatus"] = "stopped";  if( method_exists( $this, "onShutdown" ) )  $this->onShutdown();  $maxFD = count( $this->clientFD );  for( $i = 0; $i < $maxFD; $i++ )  $this->closeConnection( $i );  @socket_close( $this->initFD );  $this->sendDebugMessage( "Shutdown server." );  exit;}/*** get current amount of clients** @access public* @return int $clients amount of clients*/function getClients(){  return $this->clients;}/*** send data to a client** @access public* @param int  $clientId ID of the client* @param string $data  data to send* @param boolean $debugData flag to indicate whether data that is written to socket should also be sent as debug message*/function sendData( $clientId, $data, $debugData = true ){  if( !isset( $this->clientFD[$clientId] ) || $this->clientFD[$clientId] == NULL )  return false;  if( $debugData )  $this->sendDebugMessage( "sending: /"" . $data . "/" to: $clientId" );  if( !@socket_write( $this->clientFD[$clientId], $data ) )  $this->sendDebugMessage( "Could not write '".$data."' client ".$clientId." ( ".$this->getLastSocketError( $this->clientFD[$clientId] )." )." );}/*** send data to all clients** @access public* @param string $data  data to send* @param array $exclude client ids to exclude*/function broadcastData( $data, $exclude = array(), $debugData = true ){  if( !empty( $exclude ) && !is_array( $exclude ) )  $exclude = array( $exclude );  for( $i = 0; $i < count( $this->clientFD ); $i++ )  {  if( isset( $this->clientFD[$i] ) && $this->clientFD[$i] != NULL && !in_array( $i, $exclude ) )  {   if( $debugData )   $this->sendDebugMessage( "sending: /"" . $data . "/" to: $i" );   if( !@socket_write( $this->clientFD[$i], $data ) )   $this->sendDebugMessage( "Could not write '".$data."' client ".$i." ( ".$this->getLastSocketError( $this->clientFD[$i] )." )." );  }  }}/*** get current information about a client** @access public* @param int  $clientId ID of the client* @return array $info  information about the client*/function getClientInfo( $clientId ){  if( !isset( $this->clientFD[$clientId] ) || $this->clientFD[$clientId] == NULL )  return false;  return $this->clientInfo[$clientId];}/*** send a debug message** @access private* @param string $msg message to debug*/function sendDebugMessage( $msg ){  if( !$this->debug )  return false;  $msg = date( "Y-m-d H:i:s", time() ) . " " . $msg;  switch( $this->debugMode )  {  case "text":   $msg = $msg."/n";   break;  case "html":   $msg = htmlspecialchars( $msg ) . "<br />/n";   break;  }  if( $this->debugDest == "stdout" || empty( $this->debugDest ) )  {  echo $msg;  flush();  return true;  }  error_log( $msg, 3, $this->debugDest );  return true;}/*** return string for last socket error** @access public* @return string $error last error*/function getLastSocketError( &$fd ){  $lastError = socket_last_error( $fd );  return "msg: " . socket_strerror( $lastError ) . " / Code: ".$lastError;}function onReceiveData($ip,$data){  $this->broadcastData( $data,array(), true );}}$patServer = new patServer();$patServer->start();

 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲天堂开心观看| 亚洲a级在线观看| 97香蕉超级碰碰久久免费的优势| 国产精品一区二区三区免费视频| 久久影院资源站| 亚洲成年网站在线观看| 91精品国产综合久久香蕉922| 中文字幕久精品免费视频| 久久亚洲精品国产亚洲老地址| 92版电视剧仙鹤神针在线观看| 亚洲永久在线观看| 国产精品久久久久久久久久久新郎| 国产精品亚洲自拍| 成人观看高清在线观看免费| 日韩免费av一区二区| 69久久夜色精品国产69| 国产精品1区2区在线观看| 久久精品视频网站| 日韩亚洲精品视频| 欧美日韩999| 午夜美女久久久久爽久久| 伊人伊人伊人久久| 久久久精品国产网站| 亚洲人精品午夜在线观看| 日本三级韩国三级久久| 久久久精品视频成人| 国产精品狼人色视频一区| 97精品国产91久久久久久| 国产亚洲精品美女久久久久| 国产一区二区黄| 亚洲人高潮女人毛茸茸| 91精品久久久久久久久中文字幕| 国产精品主播视频| 伊人亚洲福利一区二区三区| 久久99热精品| 久久99热精品这里久久精品| 成人国产精品av| 九九久久久久久久久激情| 日韩精品在线观看一区| 亚洲国产精品嫩草影院久久| 国产精品99久久久久久久久| 北条麻妃一区二区在线观看| 最近中文字幕日韩精品| 7777免费精品视频| 久久久久久一区二区三区| 欧美一级大胆视频| 久久久久久69| 搡老女人一区二区三区视频tv| 性色av一区二区三区| 色青青草原桃花久久综合| 精品久久久久国产| 国产91精品高潮白浆喷水| 国产在线视频91| 2021国产精品视频| 性色av一区二区三区| 国产精品久久久久久久7电影| 亚洲aaaaaa| 国产日韩精品电影| 日韩天堂在线视频| 久久天天躁狠狠躁夜夜躁2014| 亚洲日韩第一页| 亚洲一区二区三区777| 久久久国产精品一区| 久久精品国产99国产精品澳门| 亚洲欧美日韩精品久久亚洲区| 欧美黑人狂野猛交老妇| 亚洲香蕉av在线一区二区三区| 欧美一区二区三区免费视| 欧美午夜精品久久久久久久| 成人在线视频网| 亚洲美女视频网| 欧美性猛交99久久久久99按摩| 另类少妇人与禽zozz0性伦| 亚洲欧洲国产伦综合| 国产成人综合精品在线| 日韩专区在线播放| 亚洲一区二区三区xxx视频| 久久综合国产精品台湾中文娱乐网| 国产精品成人v| xx视频.9999.com| 奇米四色中文综合久久| 日韩欧美在线看| 国产免费久久av| 日韩精品久久久久| 日韩有码在线视频| 亚洲男人天堂手机在线| 国语自产精品视频在线看| 深夜福利亚洲导航| 日韩欧美精品免费在线| 69久久夜色精品国产7777| 久久成人人人人精品欧| 操91在线视频| 另类天堂视频在线观看| 日韩av观看网址| 国产精品xxxxx| 成人春色激情网| 欧美一区二粉嫩精品国产一线天| 成人国产精品久久久久久亚洲| 青青草成人在线| 91视频-88av| 另类图片亚洲另类| 国产精品免费一区豆花| 久久久久久久久久久人体| 九九热精品视频国产| 久热精品视频在线| 色综合伊人色综合网站| 亚洲色图50p| 亚洲qvod图片区电影| 欧美激情亚洲一区| 中文国产成人精品久久一| 色多多国产成人永久免费网站| 国产精品视频一| 亚洲女人天堂视频| 97精品国产97久久久久久免费| 超碰91人人草人人干| 日韩av免费看网站| 91精品久久久久久综合乱菊| 国内外成人免费激情在线视频| 国产精品青草久久久久福利99| 日韩电影免费观看在线观看| 国产精品免费看久久久香蕉| 国产成人一区二区三区小说| 青青草精品毛片| 日韩有码在线观看| 九九热精品视频国产| 97视频在线观看免费高清完整版在线观看| 亚洲成人免费在线视频| 欧美性受xxxx白人性爽| 国产免费成人av| 在线播放日韩av| 精品中文字幕乱| 久久久电影免费观看完整版| 久久精品91久久久久久再现| 欧美精品性视频| 久久影视电视剧凤归四时歌| 欧美网站在线观看| 超碰精品一区二区三区乱码| 久久久亚洲福利精品午夜| 国产精品视频xxx| 色综合老司机第九色激情| 91久热免费在线视频| 91久久久久久久久久| 欧美孕妇孕交黑巨大网站| 欧美大片在线免费观看| 精品国产精品自拍| 欧美黄色免费网站| www日韩欧美| 久久精品国产综合| 欧美日韩亚洲视频| 欧美裸体男粗大视频在线观看| 欧美激情一区二区久久久| 欧美电影免费看| 欧美孕妇性xx| 成人av电影天堂| 57pao国产精品一区| 亚洲精品久久久久久下一站| 另类色图亚洲色图| 欧美日韩免费在线| 久久免费在线观看| 欧美激情精品久久久久久蜜臀| 久久久噜噜噜久久| 色天天综合狠狠色| 成人激情视频在线观看| 国产精品福利网|