Cli\WS::alloc PHP Метод

alloc() публичный Метод

Allocate stream socket
public alloc ( $socket ) : null
$socket resource
Результат null
    function alloc($socket)
    {
        if (is_bool($str = $this->read($socket))) {
            $this->close($socket);
            return;
        }
        // Get WebSocket headers
        $hdrs = [];
        $CRLF = "\r\n";
        $verb = NULL;
        $uri = NULL;
        foreach (explode($CRLF, trim($str)) as $line) {
            if (preg_match('/^(\\w+)\\s(.+)\\sHTTP\\/1\\.\\d$/', trim($line), $match)) {
                $verb = $match[1];
                $uri = $match[2];
            } else {
                if (preg_match('/^(.+): (.+)/', trim($line), $match)) {
                    // Standardize header
                    $hdrs[strtr(ucwords(strtolower(strtr($match[1], '-', ' '))), ' ', '-')] = $match[2];
                } else {
                    $this->close($socket);
                    return;
                }
            }
        }
        if (empty($hdrs['Upgrade']) && empty($hdrs['Sec-Websocket-Key'])) {
            // Not a WebSocket request
            if ($verb && $uri) {
                $this->write($socket, $str = 'HTTP/1.1 400 Bad Request' . $CRLF . 'Connection: close' . $CRLF . $CRLF);
            }
            $this->close($socket);
            return;
        }
        // Handshake
        $bytes = $this->write($socket, $str = 'HTTP/1.1 101 Switching Protocols' . $CRLF . 'Upgrade: websocket' . $CRLF . 'Connection: Upgrade' . $CRLF . 'Sec-WebSocket-Accept: ' . base64_encode(sha1($hdrs['Sec-Websocket-Key'] . self::Magic, TRUE)) . $CRLF . $CRLF);
        if ($bytes) {
            // Connect agent to server
            $this->sockets[] = $socket;
            $this->agents[(int) $socket] = new Agent($this, $socket, $verb, $uri, $hdrs);
        } else {
            $this->close($socket);
        }
    }