Alex Rivera | Logout

'Sec-WebSocket-Accept' header is missing in Chrome 17

Asked 2012-02-20T07:23:39.197
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)

Edit
Report

1 Answer

-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..

answered 2013-03-06T17:53:45.153

Your Answer