ホームページ >バックエンド開発 >PHPチュートリアル >PHP ソケットを使用して独自のチャット ルーム サーバーを構築する_PHP チュートリアル
/** "appName" => "patServer", /** /** /** /** /** /** /** /** /** /** /** /** /** /** /** $this->serverInfo["domain"] = $domain; set_time_limit( 0 ); /** /** $this->debug = true; /** // adress may be reused // ソケットをバインドします // 選択したポートをリッスンします
$GLOBALS["_patServerStatus"] = "running"; $this->onStart(); $this->serverInfo["status"] = "running"; $readFDs = array(); for( $i = 0; $i < count( $this->clientFD ); $i++ ) $ready = @socket_select( $readFDs, $this->null, $this->null, NULL ); $this->sendDebugMessage( "socket_select failed." ); if( in_array( $this->initFD, $readFDs ) ){ if( $this->maxClients > 0 ){
} }
http://www.bkjia.com/PHPjc/508269.html
* patServer
* PHP socket server base class
* Events that can be handled:
* * onStart
* * onConnect
* * onConnectionRefused
* * onClose
* * onShutdown
* * onReceiveData
*
* @version 1.1
* @author Stephan Schmidt
* @package patServer
*/
class patServer{
/**
* information about the project
* @var array $systemVars
*/
var $systemVars = array(
"appVersion" => "1.1",
"author" => array("Stephan Schmidt
);
* 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 = "
";
* 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["port"] = $port;
$this->serverInfo["servername"] = $this->systemVars["appName"];
$this->serverInfo["serverversion"] = $this->systemVars["appVersion"];
}
* 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->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." );
socket_setopt( $this->initFD, SOL_SOCKET, SO_REUSEADDR, 1 );
if(!@socket_bind( $this->initFD, $this->domain, $this->port ) ){
@socket_close( $this->initFD );
die ( "patServer: ポート ".$this->port." の ".$this->domain." にソケットをバインドできませんでした。" ( ".$this->getLastSocketError( $this->initFd )." )." );
}
if(!@socket_listen( $this->initFD, $this->maxQueue ) )
die( "patServer: リッスンできませんでした ( ".$this->getLastSocketError( $this ->initFd )." )."
// これにより、サーバーが正しくシャットダウンされることが保証されます
register_shutdown_function( array( $this, 「シャットダウン」 ) );
array_push( $readFDs, $this->initFD );
if( isset( $this->clientFD[$i] ) )
array_push( $readFDs, $this->clientFD[$i] );
$this->shutdown();
}
$newClient = $this->acceptConnection( $this->initFD );
if( $this->clients > $this->maxClients ){
$this->sendDebugMessage( 「接続が多すぎます。」
if(method_exists( $this, "onConnectionRefused" ) )
$this->closeConnection( $newClient );
if( --$ready continue;
// すべてのクライアントの受信データをチェックします