问题描述
更新:我解决了解码问题,感谢 pimvdb
Update: I solved the decoding problem, thanks to pimvdb
遵循解决方案(在PHP中) :
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];
}
*原始主题的开头*
我正在尝试使用 。
I'm trying to develop HTML5 WebSocket for a personal application, using hybi-17 handshake.
我是从 phpwebsocket ,我唯一改变的是握手功能,从原版到此:
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;
}
我使用了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)。
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!
这是客户端调试:
WebSocket - status 0
Welcome - status 1
Sent: hello
Disconnected - status 3
我所做的只是连接客户端并发送命令'Hello'。
正如你所看到的,服务器收到了编码数据而不是明文:为什么?
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?
我正在寻找帮助我开发html5 websocket with hybi-的人17。
I'm looking for someone who helps me to develop html5 websocket with hybi-17.
Cyaz
PS:这个(http:// stackoverflow。 com / questions / 7908306 / implementation-handshake-for-hybi-17)主题和这个(http://stackoverflow.com/questions/7912772/decoding-network-chars-html5-websocket)可能有所帮助。
PS: this (http://stackoverflow.com/questions/7908306/implementing-handshake-for-hybi-17) topic and this one (http://stackoverflow.com/questions/7912772/decoding-network-chars-html5-websocket) might help.
PPS:我测试了 Chromium 15 .0.874.106~r107270-0ubuntu0.11.04.1
PPS: I tested on Chromium 15.0.874.106~r107270-0ubuntu0.11.04.1
推荐答案
解码解决方案(感谢@pimvdb):
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];
}
这篇关于带有hybi-17的HTML5 WebSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!