Update: I solved the decoding problem, thanks to pimvdb
Follows the solution (in PHP):
$len = $masks = $data = $decoded = n
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];
}
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!