帝王谷资源网 Design By www.wdxyy.com
/** * 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();
标签:
php,socket,聊天室
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
2024年11月03日
2024年11月03日
- 明达年度发烧碟MasterSuperiorAudiophile2021[DSF]
- 英文DJ 《致命的温柔》24K德国HD金碟DTS 2CD[WAV+分轨][1.7G]
- 张学友1997《不老的传说》宝丽金首版 [WAV+CUE][971M]
- 张韶涵2024 《不负韶华》开盘母带[低速原抓WAV+CUE][1.1G]
- lol全球总决赛lcs三号种子是谁 S14全球总决赛lcs三号种子队伍介绍
- lol全球总决赛lck三号种子是谁 S14全球总决赛lck三号种子队伍
- 群星.2005-三里屯音乐之男孩女孩的情人节【太合麦田】【WAV+CUE】
- 崔健.2005-给你一点颜色【东西音乐】【WAV+CUE】
- 南台湾小姑娘.1998-心爱,等一下【大旗】【WAV+CUE】
- 【新世纪】群星-美丽人生(CestLaVie)(6CD)[WAV+CUE]
- ProteanQuartet-Tempusomniavincit(2024)[24-WAV]
- SirEdwardElgarconductsElgar[FLAC+CUE]
- 田震《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- BEYOND《大地》24K金蝶限量编号[低速原抓WAV+CUE][986M]
- 陈奕迅《准备中 SACD》[日本限量版] [WAV+CUE][1.2G]