0

I'm using a WebSocket for python and JavaScriptand until now the handshake protocol for Google Chrome was draft hybi-00. I guess Google Chrome changed the protocol to draft hybi-10 recently.

So today I updated the handshake code and now the WebSocket is succefully created and opened. On the onopen event in JavaScript, I send a simple text message:

viz.ws = new WebSocket("ws://127.0.0.1:5500");
            
viz.ws.onopen = function() {
    viz.ws.send("TEST\n");
};

My server in Python receives this data. However, it is encoded somehow and I can't get the simple text "TEST\n" I sent:

    def recv_data(self, client, count):
    
        try:
            data = client.recv(count)
        
        except:
            return False
        
        print data
        print data.decode('utf-8','ignore')

        return data.decode('utf-8', 'ignore')

The prints return this:

üàÍu┬¯é0æ║▄
u0

And they're always different, but the sent text is always TEST\n.

Also, the server receives this data, but the client isn't receiving any data sent from the server.

I read that hybi-10 uses binaries... Am I missing a data conversion in that code? I'm sorry, I'm really new to WebSockets and these protocols are messing with my head...

Community
  • 1
  • 1
Neuza
  • 117
  • 3
  • 13
  • I answered a similar question about decoding messages in hybi-10; perhaps it helps. http://stackoverflow.com/questions/7040078/not-sure-how-to-frame-data-in-websockets-draft-08/7045885#7045885 – pimvdb Sep 20 '11 at 17:00
  • By the way, Chrome 14 changed to hybi-08 (which is almost hybi-10), and the stable release has just recently been changed to 14. Chrome 15 will have support for binary frames. – pimvdb Sep 20 '11 at 17:02
  • Thanks, I think that will help a lot! =) – Neuza Sep 20 '11 at 17:16
  • I just had a project with lots of websockets and used Twisted and txwebsocket. Both hybi-10 and Hixie-76 worked with the respective Chrome versions. If you want an alternative you could try that. – Jochen Ritzel Sep 20 '11 at 17:17
  • I will have a look at that. thanks ;) – Neuza Sep 20 '11 at 17:53

1 Answers1

1

The way data is framed in HyBi (HyBi-00 is really Hixie-76) is significantly changed. The new frame format is described in this diagram.

Also, for data that is sent from the client to the server, the data is masked. The mask is the first 4 bytes of the frame payload and is decoded (and encoded actually) in place using this simple algorithm:

data[i] = data[i] XOR mask[j MOD 4]

The mask key is different with every frame which is why you are getting a different payload every time even though you are sending the same data.

If the client isn't receiving data that you sent, chances are that you aren't framing the data right. Also note that Chrome 14 and Firefox 6/7 do not yet support binary data so the opcode needs to be 1 to denote a text (UTF-8) frame.

Community
  • 1
  • 1
kanaka
  • 70,845
  • 23
  • 144
  • 140
  • Thanks! I will look into that. That's really boring because I'm trying to make a cross-browser application... first mozilla with mozWebSocket now this :P. Thank you very much for your indications! – Neuza Sep 20 '11 at 17:20
  • kanaka, I tried to use your implementation to solve this problem (with decode_hybi and encode_hybi functions) and now I succefully decode the data received from the client, and then I encode the data to send to the client (with encode_hybi function) but somehow the data is never received.. I really can't see what's wrong, I'm using exactly your code (with opcode=1 for text frames). Can you help me to figure this out? – Neuza Sep 23 '11 at 17:18
  • @Neuza. Sorry for the late reply. I suggest opening a new question and posting the relevant parts of your code and then posting back here a link to the new question so that I get notified. – kanaka Oct 02 '11 at 22:01
  • hey, no problem. I figured it out. I couldn't send the frames but the problem was in the handshake. don't know how I manage to receive frames and get a connection, but now with the handshake corrected everything works fine. Thanks for your response anyway :) – Neuza Oct 03 '11 at 11:56