开发者

Problem sending data in hybi-10 WebSockets server

I'm trying to implement the new hybi-10 protocol in a python server and for now I succeeded in the handshake and receiving data from the client (JavaScript) but now I'm having problems with sending the data to the client.

I'm using a bit of a code from websockify (encode_hybi function) to encode the frames according to this protocol. But unfortunately the client doesn't seem to receive the data, as the on message event never fires.

So the code I have is this:

def encode_hybi(self, buf, opcode = 0x01):

    buf = b64encode(buf)

    b1 = 0x80 | (opcode & 0x0f) 
    payload_len = len(buf)
    if payload_len <= 125:
        header = struct.pack('>BB', b1, payload_len)
    elif payload_len > 125 and payload_len < 65536:
        header = struct.pack('>BBH', b1, 126, payload_len)
    elif payload_len >= 65536:
        header = struct.pack('>BBQ', b1, 127, payload_len)

    print repr(header + buf)

    return header + buf

def send(self, data):
    logging.info开发者_Go百科("Message Sent: %s" % data)

    if (self.protocol == 'hixie-76'):
        self.client.send("\x00%s\xff" % data)
    elif (self.protocol =='hybi-10'):
        msg = self.encode_hybi(data)
        self.client.send(msg)

I'm sending a simple 'OK' through the socket. So after the encode_hybi function I get: '\x81\x04T0s=' which is sent to JavaScript. I have no response from it, nor errors.

I tried to send other data, for example 'OKKK'. After the encode_hybi function I get: '\x81\x08T0tLSw=='. Don't know if it helps, but with this data sent, the JavaScript presents an error:

Unrecognized frame opcode: 13.

This error appears every time the length of the data sent is bigger than 3 characters.

I really can't understand the problem. Is something wrong in the encoding?


You used \n\r\n\r\n at the end of the handshake format, but it should be \r\n\r\n. Currently the \n is part of the key.

Although I don't understand how you were still able to open a connection, it looks like removing the first \n solves the problem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜