2

Good morning everbody,

I seem to have a problem which drives me crazy, because I have absolute no idea where to search.

To gain experience with websockets I want to setup a Websocket Server with PHP and Apache. By combining the solutions of:

https://medium.com/@cn007b/super-simple-php-websocket-example-ea2cd5893575

and

https://www.php.net/manual/de/sockets.examples.php

I was able to get a working echo Server. That means I can send a message from the browser and the server answered with this message. Here is the code snippet.

do {
if (false === ($buf = socket_read($client,2048))) {
   echo "socket_read() fehlgeschlagen: Grund: " .  socket_strerror(socket_last_error($client)) . "\n";
   break 2;
}
echo $buf; // <--- this line shows "crypted" text
socket_write ($client, $buf, strlen ($buf));
} while (true);

I start this server in console with php websocket.php and a Proxy in Apache forwards the requests.

My Problem is, that the variable $buf is somehow encrypted, binary, packed or wrong encoded. It delivers non readable signs:

??ǐl??????

After sending it back to the browser everything is human readable. I studied many posts in the internet and on stackoverflow. I don't know where to start, because from the documentation it says it should be a string. All examples I found in the internet do not have a special care for "translating" this $buf string, so that I am complete confused where to search: Apache?, PHP.ini?, console, encoding, unpacking

Based on what I read I experimented a bit with:

//$talkback = "PHP: Sie haben '$buf' eingegeben.\n";
//var_dump(mb_convert_encoding($buf,"UTF-8"));
//$unpacked = unpack('Clength',socket_read($client,1));
//var_export($unpacked);
//$msg = socket_read($client, $unpacked['length']);
//echo implode(array_map("chr", $msg));

But no success. Can you help me?

Update:

After a good night of sleep I found the right question and then very quickly the answer:

The starting point for explanation:

Decoding network chars (HTML5 Websocket)

Decoding Example:

HTML5 WebSocket with hybi-17

And a code example for encoding/decoding:

http://www.abrandao.com/2013/06/websockets-html5-php/

mapaonso
  • 33
  • 1
  • 4

0 Answers0