Anshuman Biswas
Anshuman Biswas

Reputation: 789

Websockets not working on Google Chrome

I have a server in PHP which binds to a port and listens to sockets. My server is started in a PHP script with:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, $port);

It then listens to the port:

socket_listen($socket);

When a HTTP message arrives from the client, the server reads the header:

$header = socket_read($socket_new,1024);

and then stores the connection in a Memcache storage. This works for most browsers including safari, firefox and Chrome's Canary. However, it doesn't work on chrome. The browser throws an error message:

WebSocket connection to 'ws://xyz.com:9001/chat_server.php' failed: Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value

My version of Chrome is: Version 38.0.2125.111 m (64-bit)

Upvotes: 2

Views: 4689

Answers (1)

Ali Nikravesh
Ali Nikravesh

Reputation: 86

We had the same issue and we could solve it by increasing the "maximum number of bytes" parameter in the socket_read() function. You can try

socket_tead($socket_new, 2048);

The reason is that websocket header in chrome sometimes is greater that 1024 bytes. So, when your server reads 1024 bytes, it does not get Sec-Websocket-Key parameter and it can not generate valid Sec-Websocket-Accept value.

You can also use fsockopen() and fread() instead of socket_read() function.

Upvotes: 7

Related Questions