dongzhiju0324
2014-11-07 23:39
浏览 297
已采纳

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)

图片转代码服务由CSDN问答提供 功能建议

我在PHP中有一个绑定到端口并侦听套接字的服务器。 我的服务器在PHP脚本中启动:

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

然后它监听端口:

  socket_listen  ($ socket); 
   
 
 

当HTTP消息从客户端到达时,服务器会读取标头:

  $ header = socket_read($ socket_new,1024); 
   
 
 

然后将连接存储在Memcache存储中。 这适用于大多数浏览器,包括safari,firefox和Chrome的Canary。 但是,它不适用于chrome。 浏览器抛出一条错误消息:

WebSocket连接到'ws://xyz.com:9001 / chat_server.php' 失败:WebSocket握手期间出错: 不正确 'Sec-WebSocket-Accept'标题值

我的Chrome版本为:版本38.0.2125.111 m(64位) \ n

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsk88199 2014-11-07 23:50
    已采纳

    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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题