Search code examples
phpphpwebsocket

While loop speed in websocket php server


I am using the following websocket php class as my server: PHPWebSocket. It works well in every way I could have hoped for, but I've run into a bit of a problem.

I want to update the positions of connected clients (they can walk around) at a rate of maybe every 0.3 seconds. I figured it would be a simple matter of looking for the while loop and adding a heartbeat event there, but here it becomes tricky.

    // 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
}

To me it doesn't seem like there is any specific timer that says it should only run once per second, but it does. I am not talking about the ping check, even if I place my heartbeat call directly after the while (isset($this->wsRead[0])) {, it will only trigger once per second, and it has me completely stumped.

Am I looking in the wrong place? Is there something about PHP's websocket implementation that slows down this while loop?


Solution

  • You have a one second timeout in your socket_select call - that's probably where your delay is introduced. If you want that call to be non-blocking, you can pass a zero for that timeout.