而在websocket php服务器的循环速度

时间:2013-01-25 14:26:43

标签: php phpwebsocket

我使用以下websocket php类作为我的服务器:PHPWebSocket。它能以我所希望的各种方式运作良好,但我遇到了一些问题。

我想以每0.3秒的速度更新已连接客户端(他们可以四处走动)的位置。我认为在寻找while循环并在那里添加心跳事件是一件简单的事情,但这里变得棘手。

    // server state functions
function wsStartServer($host, $port) {
    if (isset($this->wsRead[0])) return false;

    if (!$this->wsRead[0] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) {
        return false;
    }
    if (!socket_set_option($this->wsRead[0], SOL_SOCKET, SO_REUSEADDR, 1)) {
        socket_close($this->wsRead[0]);
        return false;
    }
    if (!socket_bind($this->wsRead[0], $host, $port)) {
        socket_close($this->wsRead[0]);
        return false;
    }
    if (!socket_listen($this->wsRead[0], 10)) {
        socket_close($this->wsRead[0]);
        return false;
    }

    $this->log("Server starting");

    $write = array();
    $except = array();

    $nextPingCheck = time() + 1;
    while (isset($this->wsRead[0])) {

        $changed = $this->wsRead;
        $result = socket_select($changed, $write, $except, 1);
                     **beat()**
        if ($result === false) {
            socket_close($this->wsRead[0]);
            return false;
        }
        elseif ($result > 0) {
            foreach ($changed as $clientID => $socket) {
                if ($clientID != 0) {
                    // client socket changed
                    $buffer = '';
                    $bytes = @socket_recv($socket, $buffer, 4096, 0);

                    if ($bytes === false) {
                        // error on recv, remove client socket (will check to send close frame)
                        $this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR);
                    }
                    elseif ($bytes > 0) {
                        // process handshake or frame(s)
                        if (!$this->wsProcessClient($clientID, $buffer, $bytes)) {
                            $this->wsSendClientClose($clientID, self::WS_STATUS_PROTOCOL_ERROR);
                        }
                    }
                    else {
                        // 0 bytes received from client, meaning the client closed the TCP connection
                        $this->wsRemoveClient($clientID);
                    }
                }
                else {
                    // listen socket changed
                    $client = socket_accept($this->wsRead[0]);
                    if ($client !== false) {
                        // fetch client IP as integer
                        $clientIP = '';
                        $result = socket_getpeername($client, $clientIP);
                        $clientIP = ip2long($clientIP);

                        if ($result !== false && $this->wsClientCount < self::WS_MAX_CLIENTS && (!isset($this->wsClientIPCount[$clientIP]) || $this->wsClientIPCount[$clientIP] < self::WS_MAX_CLIENTS_PER_IP)) {
                            $this->wsAddClient($client, $clientIP);
                        }
                        else {
                            socket_close($client);
                        }
                    }
                }
            }
        }

        if (time() >= $nextPingCheck) {
            $this->wsCheckIdleClients();
            $nextPingCheck = time() + 1;
        }
    }

    return true; // returned when wsStopServer() is called
}

对我而言,似乎没有任何特定的计时器表明它应该每秒只运行一次,但确实如此。我不是在谈论ping检查,即使我在while(isset($ this-&gt; wsRead [0])){之后直接放置我的心跳呼叫,它每秒只会触发一次,它让我完全难倒

我在寻找错误的地方吗?有没有关于PHP的websocket实现的东西会减慢这个while循环?

1 个答案:

答案 0 :(得分:1)

您的socket_select电话中有一秒钟超时 - 这可能是您的延迟时间。如果您希望该调用是非阻塞的,则可以为该超时传递零。