ElephantIO\Engine\SocketIO\Version1X::upgradeTransport PHP Method

upgradeTransport() private method

Upgrades the transport to WebSocket
private upgradeTransport ( )
    private function upgradeTransport()
    {
        $query = ['sid' => $this->session->id, 'EIO' => $this->options['version'], 'use_b64' => $this->options['use_b64'], 'transport' => static::TRANSPORT_WEBSOCKET];
        $url = sprintf('/%s/?%s', trim($this->url['path'], '/'), http_build_query($query));
        $key = base64_encode(sha1(uniqid(mt_rand(), true), true));
        $origin = '*';
        $headers = isset($this->context['headers']) ? (array) $this->context['headers'] : [];
        foreach ($headers as $header) {
            $matches = [];
            if (preg_match('`^Origin:\\s*(.+?)$`', $header, $matches)) {
                $origin = $matches[1];
                break;
            }
        }
        $request = "GET {$url} HTTP/1.1\r\n" . "Host: {$this->url['host']}\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "Sec-WebSocket-Key: {$key}\r\n" . "Sec-WebSocket-Version: 13\r\n" . "Origin: {$origin}\r\n";
        if (!empty($this->cookies)) {
            $request .= "Cookie: " . implode('; ', $this->cookies) . "\r\n";
        }
        $request .= "\r\n";
        fwrite($this->stream, $request);
        $result = fread($this->stream, 12);
        if ('HTTP/1.1 101' !== $result) {
            throw new UnexpectedValueException(sprintf('The server returned an unexpected value. Expected "HTTP/1.1 101", had "%s"', $result));
        }
        // cleaning up the stream
        while ('' !== trim(fgets($this->stream))) {
        }
        $this->write(EngineInterface::UPGRADE);
        //remove message '40' from buffer, emmiting by socket.io after receiving EngineInterface::UPGRADE
        $this->read();
    }