HTML5 WebSocket 与 hybi-17

发布于 2024-12-12 13:01:10 字数 3409 浏览 0 评论 0原文

更新:我解决了解码问题,感谢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];
}

***原始主题的开始***

我正在尝试开发用于个人应用程序的 HTML5 WebSocket,使用 hybi-17 握手

我从 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;
}

我用了phpwebsocket源码,都是针对客户端的(http://code.google.com/p/phpwebsocket /source/browse/trunk/+phpwebsocket/client.html)和服务器http://code.google.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 的人。

Cyaz

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

PPS:我在 Chromium 15.0.874.106~r107270-0ubuntu0.11.04.1 上进行了测试

Update: I solved the decoding problem, thanks to pimvdb

Follows the solution (in 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];
}

*** Begin of the original topic ***

I'm trying to develop HTML5 WebSocket for a personal application, using hybi-17 handshake.

I'm started with phpwebsocket and the only thing I changed is the handshake function, from the original to this:

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;
}

I used phpwebsocket source code, both for the client (http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/client.html) and the server (http://code.google.com/p/phpwebsocket/source/browse/trunk/+phpwebsocket/server.php).

At this point, I tested the application. Follows the server debug:

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!  

And this is the client debug:

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

What I did was simply connect the client and send the command 'Hello'.
As you can see, server received encoded data and not plaintext: why?

I'm looking for someone who helps me to develop html5 websocket with hybi-17.

Cyaz

PS: this (Implementing handshake for hybi-17) topic and this one (Decoding network chars (HTML5 Websocket)) might help.

PPS: I tested on Chromium 15.0.874.106~r107270-0ubuntu0.11.04.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-12-19 13:01:11

这个编码功能很棒,只是 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];

preg_match("#Origin: (.*?)\r\n#", $buffer, $match) && $origin = $match[1];

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

This encode function is great except that chrome 19 doesn't like the data being masked. I get this error after sending the first message to server:
"A server must not mask any frames that it sends to the client."

I couldn't figure out how to not mask the data being sent to the server until I found a github example that works and has a flag you can set to not mask the data frames.
https://github.com/lemmingzshadow/php-websocket

I had found this updated version of the phpwebsocket code:
http://www.wilky.it/phpwebsocket-new-version/
One thing you need to fix in this to make it work is replace this line at line 234:
preg_match ("#Sec-WebSocket-Origin: (.*?)\r\n#", $buffer, $match) && $origin = $match[1];
with
preg_match ("#Origin: (.*?)\r\n#", $buffer, $match) && $origin = $match[1];

Then to fix the error in chrome: "A server must not mask any frames that it sends to the client." do the following:
I fixed the replaced the encode function with the one in the connection.php file in lemmingzshadow's github and it started working. The function is called: hybi10Encode in the \server\lib\WebSocket\connection.php file.
change this parameter in the function encode: $masked = true to $masked = false
So we have 2 versions that work now in chrome and firefox 12!

淡淡绿茶香 2024-12-19 13:01:10

解码解决方案(感谢@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];
}

Decode solution (thanks to @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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文