开发者

Data encoding in new protocol handshake hybi-10

开发者 https://www.devze.com 2023-04-06 08:15 出处:网络
I\'m using a WebSocke开发者_StackOverflow中文版t 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

I'm using a WebSocke开发者_StackOverflow中文版t 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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号