1

更新:我解决了解码问题,感谢pimvdb

遵循解决方案(在 PHP 中):

$len = $masks = $data = $decoded = null;

$len = ord ($buffer[1]) & 127;

if ($len === 126) {
  $masks = substr ($buffer, 4, 4);
  $data = substr ($buffer, 8);
}
else if ($len === 127) {
  $masks = substr ($buffer, 10, 4);
  $data = substr ($buffer, 14);
}
else {
  $masks = substr ($buffer, 2, 4);
  $data = substr ($buffer, 6);
}

for ($index = 0; $index < strlen ($data); $index++) {
  $decoded .= $data[$index] ^ $masks[$index % 4];
}

*** 原题目开始***

我正在尝试使用hybi-17 handshake为个人应用程序开发 HTML5 WebSocket 。

我从phpwebsocket开始,唯一改变的是握手功能,从原来的到这个:

function dohandshake($user,$buffer){
  $key = null;
  
  console("\nRequesting handshake...");
  console($buffer);
  console("Handshaking...");
  
  preg_match ("#Sec-WebSocket-Key: (.*?)\r\n#", $buffer, $match) && $key = $match[1];
  
  $key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  $key = sha1 ($key);
  $key = pack ('H*', $key);
  $key = base64_encode ($key);
  
  $upgrade = 
    "HTTP/1.1 101 Switching Protocols\r\n" .
    "Upgrade: websocket\r\n" .
    "Connection: Upgrade\r\n" .
    "Sec-WebSocket-Accept: {$key}\r\n\r\n";
  
  socket_write($user->socket,$upgrade.chr(0),strlen($upgrade.chr(0)));
  $user->handshake=true;
  console($upgrade);
  console("Done handshaking...");
  return true;
}

我为客户端http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/client.html)和服务器( http://code.google)使用了phpwebsocket源代码。 com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/server.php)。

此时,我测试了应用程序。遵循服务器调试:

Server Started : 2011-10-30 13:45:41
Master socket  : Resource id #4
Listening on   : localhost port 12345

Resource id #5 CONNECTED!

Requesting handshake...
GET /phpwebsocket/server.php HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:12345
Sec-WebSocket-Origin: http://localhost
Sec-WebSocket-Key: +S/J2jcp/UKIS1HTW0n1/w==
Sec-WebSocket-Version: 8


Handshaking...
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: LEULWidwXDxY02iv3O+xksrxFz4=


Done handshaking...
< ��z�}p
> ��z�}p not understood
Resource id #5 DISCONNECTED!  

这是客户端调试:

WebSocket - status 0
Welcome - status 1
Sent: hello
Disconnected - status 3

我所做的只是连接客户端并发送命令'Hello'。如您所见,服务器收到的是编码数据而不是明文:为什么?

我正在寻找帮助我使用 hybi-17 开发 html5 websocket 的人。

恰兹

PS:这个为 hybi-17 实现握手)主题和这个解码网络字符(HTML5 Websocket))可能会有所帮助。

PPS:我在Chromium 15 .0.874.106~r107270-0ubuntu0.11.04.1上测试过

4

2 回答 2

1

解码解决方案(感谢@pimvdb):

$len = $masks = $data = $decoded = null;

$len = ord ($buffer[1]) & 127;

if ($len === 126) {
  $masks = substr ($buffer, 4, 4);
  $data = substr ($buffer, 8);
}
else if ($len === 127) {
  $masks = substr ($buffer, 10, 4);
  $data = substr ($buffer, 14);
}
else {
  $masks = substr ($buffer, 2, 4);
  $data = substr ($buffer, 6);
}

for ($index = 0; $index < strlen ($data); $index++) {
  $decoded .= $data[$index] ^ $masks[$index % 4];
}
于 2012-05-24T19:05:11.420 回答
0

这个编码功能很棒,除了 chrome 19 不喜欢被屏蔽的数据。将第一条消息发送到服务器后,我收到此错误:“服务器不得屏蔽它发送给客户端的任何帧。”

我无法弄清楚如何不屏蔽发送到服务器的数据,直到我找到一个有效的 github 示例并且有一个可以设置为不屏蔽数据帧的标志。 https://github.com/lemmingzshadow/php-websocket

我发现了 phpwebsocket 代码的更新版本:http: //www.wilky.it/phpwebsocket-new-version/ 你需要修复的一件事是在第 234 行替换这一行: preg_match (" #Sec-WebSocket-Origin: (.*?)\r\n#", $buffer, $match) && $origin = $match[1]; with preg_match ("#Origin: (.*?)\r\n#", $buffer, $match) && $origin = $match[1];

然后修复 chrome 中的错误:“服务器不得屏蔽它发送给客户端的任何帧。” 执行以下操作:我用 lemmingzshadow 的 github 中的 connection.php 文件修复了替换的编码函数,它开始工作。函数调用:\server\lib\WebSocket\connection.php 文件中的 hybi10Encode。在函数 encode 中更改此参数: $masked = true 到 $masked = false 所以我们有 2 个版本现在可以在 chrome 和 firefox 12 中使用!

于 2012-06-30T05:43:49.393 回答