Chrome handwritten letter 20

I am using a PHP Websocket server from lemmingzshadow ( web ). So far, everything has worked perfectly.

After upgrading to xrome 20, if I want to do a handshake with the server, it ends with this error

Error during WebSocket handshake: Sec-WebSocket-Protocol mismatch 

Chrome Headers 20

 GET /demo HTTP/1.1 Upgrade: websocket Connection: Upgrade Host: gomokulive.eu:8001 Origin: http://www.gomokulive.eu Sec-WebSocket-Key: s+AMQQu4Q10xH2AKy49byg== Sec-WebSocket-Version: 13 Sec-WebSocket-Extensions: x-webkit-deflate-frame 

Headers sent back:

 HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: dMCVYKkF5VRrIouWFW7EYdvfD28= Sec-WebSocket-Protocol: demo 

I think the problem is with the "Sec-WebSocket-Extensions: x-webkit-deflate-frame" header from Chrome 20.

Any idea how to make it work again?

+4
source share
3 answers

The server violates the WebSocket protocol. Most likely, Chrome simply adheres to the standard more correctly in version 20 and detects an error on the server.

The problem is that the server sends a “Sec-WebSocket-Protocol” header in response, but this is only allowed if the client sends the same header in the request. If the client does not send Sec-WebSocket-Protocol, the server should omit the header in the response.

See / subprotocol / description on page 22 in Section 4.2.2 rfc6455

+14
source

A quick fix for php-websocket would be:

 $response.= "Sec-WebSocket-Accept: " . $secAccept . "\r\n"; if (isset($headers['Sec-WebSocket-Protocol'])) { $response.= "Sec-WebSocket-Protocol: " . substr($path, 1) . "\r\n"; } $response .= "\r\n"; 
+3
source

The EASY way to fix adds Sec-WebSocket-Accept information when do_handshake the code as shown below:

  list($resource,$host,$origin,$key) = $this->getheaders($buffer); $accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true)); $upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" . "Upgrade: WebSocket\r\n" . "Connection: Upgrade\r\n" . "WebSocket-Origin: {$origin}\r\n" . "WebSocket-Location: ws://{$host}{$resource}\r\n". "Sec-WebSocket-Accept: " . $accept . "\r\n\r\n"; $this->handshakes[$socket_index] = true; socket_write($socket,$upgrade,strlen($upgrade)); 

Where

$accept = base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));

$ key Sec-WebSocket-Key received from $ buffer, you can print_r ($ buffer) look.

Hope this can solve your problem.

+1
source

All Articles