2

I'm trying to develop a webchat with HTML5 websocket (with hybi-17 protocol) but I've some problems with chars decoding. This is what I send through the client (user-agent: Firefox 7):

var socket = new MozWebSocket ('ws://localhost/server.php');
socket.onopen = function () {
  alert ('Opened!');
}

Then, I send these data:

socket.send ('Hello');
socket.send ('World');

And this is the server-side code:

$bytes = @socket_recv ($socket, $buffer, BUFSIZE, 0);

if (($bytes == 0) || ($bytes == 2)) {
  this->disconnect ($socket);
}
else {
  echo $buffer;
}

While this is the data recevied echoed:

��6S~g?Y (Hello)
���~����� (World)

As you can see, the socket is opened and data travels from the client to the server. The server works with PHP5 and uses normal socket functions to build the connection.

How can I decode that unreadable string in a human readable one?

Thanks in advance.

Wilk
  • 7,873
  • 9
  • 46
  • 70

2 Answers2

5

You have made one of the most common errors people make when they first start writing code that uses TCP -- you forgot to implement the protocol!

In your case, you forgot it in the server. The client already has a WebSocket implementation, and you request it by creating a 'MozWebSocket' object. The WebSocket specification says, "Each frame starts with a 0x00 byte, ends with a 0xFF byte, and contains UTF-8 data in between." Where's the code in the server to find the start of a frame and the end of a frame? Where the code to discard the 0xFF byte?

You actually have to implement the protocol. The protocol specification tells you how to decode the received data. (In your case, the data you are seeing as junk is most likely part of the protocol handshake -- the part that looks like ^n:ds[4U in this description of the handkshake phase.)

I'm pretty sure your method of invoking a PHP script to handle the WebSocket call through the web server will not work. That is, unless your web server knows how to do this -- WaterSpout and phpdaemon do.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • I just edited my first topic with more details. I think that the handshake phase works well and I'm looking for a simple solution. What I have to do to decode data sent from the client? If I'm wrong with the protocol, what kind of change I've to do? Thanks. – Wilk Oct 27 '11 at 14:15
  • You actually have to implement the protocol. You can either do it your self or us an [existing PHP implementation](http://code.google.com/p/phpwebsocket/). – David Schwartz Oct 27 '11 at 21:23
  • No, I can't use an existing PHP implementation like phpwebsocket because is incompatible with hybi-17 handshake. In fact, both FF 7 and Chrome 14 send a single Key (Sec-Websocket-Key) with other different parameters. Just for example, follows the header of a hybi-17 request: GET server.php HTTP/1.1 Host: localhost:12345 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 Connection: keep-alive, Upgrade Sec-WebSocket-Version: 8 Sec-WebSocket-Origin: http://localhost Sec-WebSocket-Key: CII8U3hNx3dNF/+GKerVgQ== Upgrade: websocket So, the my problem still reamins. – Wilk Oct 28 '11 at 17:49
  • I would recommend finding an existing PHP implementation of the WebSocket protocol and using that. – David Schwartz Oct 28 '11 at 21:01
0

Using the base server.php and client.html from http://code.google.com/p/phpwebsocket/ along with modification from HTML5 WebSocket with hybi-17 to deal with the single key standard I can successfully send one message to the server before it closes unexpectedly. It's at least a step closer just not sure as of yet why it closes after one successful message.

Community
  • 1
  • 1
Brian
  • 2,294
  • 6
  • 30
  • 51