更新: 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ハンドシェイクを使用して、個人用アプリケーション用のHTML5WebSocketを開発しようとしています。
私は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 / browser / 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を使用してhtml5WebSocketを開発するのを手伝ってくれる人を探しています。
Cyaz
PS:this(hybi-17のハンドシェイクの実装)トピックとthis(Decoding network chars(HTML5 Websocket))が役立つかもしれません。
PPS:Chromium 15 .0.874.106〜r107270-0ubuntu0.11.04.1でテストしました