加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php socket实现的聊天室代码分享

发布时间:2020-05-24 07:39:14 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了php socket实现的聊天室代码分享,本文实现代码来自国外友人,需要的朋友可以参考下

<div class="jb51code">
<pre class="brush:php;">
/**

  • 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,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

  • (编辑:安卓应用网)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读