'Sec-WebSocket-Accept' header is missing in Chrome 17
Asked Answered
S

3

12

Edit: I tried this phpwebsocket: http://www.wilky.it/Shared/phpwebsocket.zip and it works in Firefox, but my question still remains: how do I get websockets to work with a php server in Chrome 17?


I'm following the tutorial here: http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/

It appears as though the client connects, and then immediately disconnects. I noticed this error in the console:

Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing

I'm trying it in Chrome 17.0.963.56 on my WAMP localhost with the php_sockets extension enabled.

I saw mentioned somewhere that Chrome had changed what it supported, but it didn't go in depth on how to fix it. I was hoping someone could step me through it. (I'm brand new to websockets).

Server:

{PATH}>php startDaemon.php

2012-02-20 07:02:51 System: Socket Resource id #7 created.

2012-02-20 07:02:51 System: Socket bound to localhost:8000.

2012-02-20 07:02:51 System: Start listening on Socket.

2012-02-20 07:03:01 WebSocket: Resource id #8 CONNECTED!

2012-02-20 07:03:01 WebSocket: Requesting handshake…

2012-02-20 07:03:01 WebSocket: Handshaking…

2012-02-20 07:03:01 WebSocket: Done handshaking…

2012-02-20 07:03:01 WebSocket: Resource id #8 disconnected!

Client:

Socket Status: 0

Socket Status: 3 (Closed)

Sideman answered 20/2, 2012 at 7:23 Comment(4)
It would be helpful if you could print the received handshake and the whole response being sent. The error indicates that the Accept value is missing but it's hard to know without seeing the handshake request and response. I do note that the server you linked is sending back to many fields in the response.Florist
I'm using exactly the code from that link. I'm not sure exactly what you're asking for?Sideman
I'm asking for you to add instrumentation to the code to print out the handshake request (from the browser) and the response (from the server).Florist
I thinks you are using this project: github.com/GeorgeNava/phpwebsocket , In my opinion, use this forked project: github.com/esromneb/phpwebsocket instead, This work well and no missing Sec-WebSocket-Accept.Feaster
B
1

I have the same problem (and I do not seem to be able to post a comment here, so I post a reply).

Actually, I just downloaded and tested phpwebsocket.

On safari 5.1.4, it works just fine.

On Chrome 17, I got the same error in the script log console:

Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missing

So, in websocket.class.php, I added to the header returned by server:

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

And I get the error:

Error during WebSocket handshake: Sec-WebSocket-Accept mismatch

Now, the header received by the server is:

GET /websocket/server.php HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:12345
Origin: http://localhost:8888
Sec-WebSocket-Key: OqMJI0t/cOl6d6JNE+Op0g==
Sec-WebSocket-Version: 13

And the header sent back by the server is:

HTTP/1.1 101 WebSocket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Origin: http://localhost:8888
Sec-WebSocket-Location: ws://localhost:12345/websocket/server.php
Sec-WebSocket-Accept: ZjY5ODliNTViYzJlOTNkMjk4OTg3Y2U2NjQ3MTBlZjZiNzliYzk4Yg==

The Sec-WebSocket-Accept seems good, but still there is a mismatch error. Do you see a mistake somewhere? Maybe the protocol has changed to calculate the Sec-WebSocket-Accept, but I don't find it... Thanks for your help!

Edit: Here seems to be the solution (for me, at least): adding the parameter true to the SHA1 function, as found in files given in this issue thread. So, the Sec-WebSocket-Accept must be found like this:

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

And, Sec-WebSocket-Key1 and Sec-WebSocket-Key2 does not seem to be present anymore in the client request, instead, $key must be extracted from the header: "Sec-WebSocket-Key".

New issue: It seems too that even if the web socket connection now works on the handshake, it disconnects when the first message is sent.

Blim answered 17/3, 2012 at 16:37 Comment(0)
Y
0

I noticed that in the console of Chrome 19: A server must not mask any frames that it sends to the client. Maybe this is the problem. It disconnects as soon as a message is sent. It works fine in Firefox.

I fixed this websocket problem and it works in chrome now. First I used:

Then I used the encode function from: https://github.com/lemmingzshadow/php-websocket

I fixed the replaced the encode function with the one in the connection.php file in lemmingzshadow’s github and it started working. The function is called: hybi10Encode in the \server\lib\WebSocket\connection.php file.

change this parameter in the function encode: $masked = true to $masked = false

Yester answered 30/6, 2012 at 4:13 Comment(0)
E
-1

An EASY way to fix is add Sec-WebSocket-Accept information when do_handshake, code as 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 is Sec-WebSocket-Key got from $buffer, you can print_r($buffer) to have a look.

Hope this can solve your problem..

Ellington answered 6/3, 2013 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.