Websocket handshake connection error in PHP

161
July 02, 2018, at 5:10 PM

Error showing in console : WebSocket connection to 'ws://localhost/socket/php-socket.php/wss' failed: Error during WebSocket handshake: Unexpected response code: 200

PHP Code :

    $newSocket = socket_accept($socketResource);
    $clientSocketArray[] = $newSocket;
    $header = socket_read($newSocket, 1024);
    $chatHandler->doHandshake($header, $newSocket, HOST_NAME, PORT);
enter code here

Handshake function :

function doHandshake($received_header,$client_socket_resource, $host_name, $port) {
    $headers = array();
    $lines = preg_split("/\r\n/", $received_header);
    foreach($lines as $line)
    {
        $line = chop($line);
        if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
        {
            $headers[$matches[1]] = $matches[2];
        }
    }
    $secKey = $headers['Sec-WebSocket-Key'];
    $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
    $buffer  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
    "Upgrade: websocket\r\n" .
    "Connection: Upgrade\r\n" .
    "WebSocket-Origin: $host_name\r\n" .
    "WebSocket-Location: ws://$host_name:$port/demo/shout.php\r\n".
    "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
    socket_write($client_socket_resource,$buffer,strlen($buffer));
}

Javascript Code :

    var websocket = new WebSocket("ws://localhost/socket/php-socket.php"); 
    websocket.onopen = function(event) { 
        showMessage("<div class='chat-connection-ack'>Connection is established!</div>");       
    }
READ ALSO
Why does php password_verify return false

Why does php password_verify return false

I'm trying to make a login system with php and mysqlI verified that varchar(100) of password column in database is greater than 60 and went through the code several times, but still getting false from password_verify

130
I want to remain on same page after i report a comment

I want to remain on same page after i report a comment

so i want to make a little blog in php and mysql using a mvc arhitecturei want to add a function where you can report a comment(everything went well but i can't figure out how to remain on the same page after reporting)

118
PHPUnit to Delay assert

PHPUnit to Delay assert

im make a Publish Subscribe testim trying to using sleep(3) to delay for queue:worker until finish consume

158
php doesn&#39;t execute on wamp installation

php doesn't execute on wamp installation

I installed wamp64 on my computerPath is: c:/wamp64 I had some errors saying that php path variable were defined, so i deleted those

138