dongzhiju0324
dongzhiju0324
2014-11-07 23:39

Web浏览器无法在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)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsk88199 dsk88199 7年前

    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.

    点赞 评论 复制链接分享

为你推荐