更新:我解決解碼問題,這要歸功於pimvdbHTML5的WebSocket與hybi-17
遵循解決方案(在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 handshake。
我開始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/軀幹/ + 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
我所做的只是客戶端連接,併發送命令「你好」。如您所見,服務器收到編碼數據而不是純文本:爲什麼?
我在找人幫我開發帶hybi-17的html5 websocket。
Cyaz
PS:這(http://stackoverflow.com/questions/7908306/implementing-handshake-for-hybi-17)的主題,這一個(HTTP://計算器。 com/questions/7912772/decode-network-chars-html5-websocket)可能會有所幫助。
PPS:我上鉻15測試 .0.874.106〜r107270-0ubuntu0.11.04.1
您需要解碼您獲取的消息; hybi包括一個相對複雜的框架機制(與hixie草案相比)。您不能「只」讀取傳入的字節。我發佈了一些解碼算法以前在http://stackoverflow.com/questions/7040078/how-to-deconstruct-data-frames-in-websockets-hybi-08/7045885#7045885。 – pimvdb
謝謝pimvdb!我做的!現在,您可以在原始主題的頂部查看解決方案;)Cyaz – Wilk
對不起pimvdb,但我有一些有效負載長度的問題。好吧,當我試圖用$ msg [1]&127(或0x7F)獲得長度時,我總是得到'0'。在我的特殊情況下,緩衝區不是數組而是字符串:所以代碼將是substr($ msg,1,1)和127.它不起作用。爲什麼?爲什麼我無法獲得您的建議後的有效載荷長度?有任何想法嗎? – Wilk