2011-10-30 23 views
0

更新:我解決解碼問題,這要歸功於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

+1

您需要解碼您獲取的消息; hybi包括一個相對複雜的框架機制(與hixie草案相比)。您不能「只」讀取傳入的字節。我發佈了一些解碼算法以前在http://stackoverflow.com/questions/7040078/how-to-deconstruct-data-frames-in-websockets-hybi-08/7045885#7045885。 – pimvdb

+0

謝謝pimvdb!我做的!現在,您可以在原始主題的頂部查看解決方案;)Cyaz – Wilk

+0

對不起pimvdb,但我有一些有效負載長度的問題。好吧,當我試圖用$ msg [1]&127(或0x7F)獲得長度時,我總是得到'0'。在我的特殊情況下,緩衝區不是數組而是字符串:所以代碼將是substr($ msg,1,1)和127.它不起作用。爲什麼?爲什麼我無法獲得您的建議後的有效載荷長度?有任何想法嗎? – Wilk

回答

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]; 
} 
+0

你(和@pimvdb)剛剛救了我的命。謝謝! – boyd

0

此編碼功能是大除了鉻19不喜歡被屏蔽的數據。將第一條消息發送到服務器後出現此錯誤: 「服務器不得掩蓋它發送給客戶端的任何幀。」

我找不到如何屏蔽發送到服務器的數據,直到我找到一個可以工作的github示例並且有一個標誌可以設置爲不屏蔽數據幀。 https://github.com/lemmingzshadow/php-websocket

我已經找到了phpwebsocket代碼的更新版本: 你需要在這個修復,使其工作http://www.wilky.it/phpwebsocket-new-version/ 有一件事是線替換該行234: 的preg_match(「#仲WebSocket的原產地:(。*?)\ 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中運行!