向websocket客户端发送消息时,压缩位必须为0 [英] Compressed bit must be 0 when sending a message to websocket client

查看:317
本文介绍了向websocket客户端发送消息时,压缩位必须为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在python中编写一个websocket服务器,并且在将消息发送回客户端时遇到了问题。使用chrome的javascript控制台,我可以找到此错误消息:

I'm writing a websocket server in python, and I've run into a problem when sending messages back to the client. Using chrome's javascript console, I can get as far as finding this error message:


如果没有协商的deflate-frame扩展,压缩位必须为0

Compressed bit must be 0 if no negotiated deflate-frame extension

但是比我更远,我找不到任何关于实际含义的文件。您可以在此处找到此问题(和代码)的开头。我有点不知所措。我已尝试使用第一个字节,将其更改为全0,但根据这个,第二位,在我的情况下是0,如果文件是正确的;

But farther than that I can't find any documentation on what that actually means. You can find the start of this problem (and the code) here. I'm kind of at a loss. I've tried playing around with the first byte, changing it to all 0's, but according to this, the second bit, which is 0 in my case, should be the "compressed bit" if this documentation is correct;


WebSocket帧
头中的一个保留位RSV1被分配用于控制每个
消息的压缩应用。

"One reserved bit RSV1 in the WebSocket frame header is allocated to control application of compression for each message."

所以我不确定从哪里开始。在firefox中,错误只是与ws:http:// localhost:8080 /的连接在页面加载时被中断。错误。

So I'm not entirely sure where to go from here. In firefox the error is simply a "The connection to ws://localhost:8080/ was interrupted while the page was loading." error.

编辑:这是收到邮件时的解析代码:

Here is the parse code when receiving a message:

def parse( self, data, client_socket ):
    display_server_message( "Received message:\n%s" % str( data ) )
    byte_array = bytearray( data )
    mask = []

    message_string = ""
    for i in range( 0, len( byte_array ) ):
        b = byte_array[ i ]
        if i > 0 and i < 2:
            b = b & 0b01111111
        if i > 1 and i < 6:
            mask.append( byte_array[ i ] )
        if i > 5:
            b = b ^ mask[ (i - 2) % 4 ]
            message_string = message_string + chr( b )

    display_server_message( "Decoded Message:\n%s" % message_string )
    if ( message_string == "a test message" ):
        time.sleep( 5 )
        self.send_message( "Hello, how are you?", client_socket )


    pat = re.compile( "Sec-WebSocket-Key: (\S+)", re.MULTILINE )

    for match in pat.findall( data ):
        magic_string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

        message = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\n\r\n" % base64.standard_b64encode( hashlib.sha1( match + magic_string ).digest() )

        client_socket.send( message )
        display_server_message( "Message Sent:\n%s" % message )


推荐答案

这是与您的其他问题相同的答案: WebSocket onmessage not not解雇

This is the same answer as for your other question: WebSocket onmessage not firing

您的问题是您使用的是 bytes 函数python 2.X但希望它能像python 3.X一样工作

You're problem is that you are using the bytes function in python 2.X but expecting it to work the way it does in python 3.X

python2.6.6:

python2.6.6:

>>> bytes([129, 19])
'[129, 19]'

python 3 :

python 3:

>>> bytes([129, 19])
b'\x81\x13'

通过在服务器代码中替换以下发送,它在python2.X中适用于我:

By replacing the send with the following in your server code it works for me in python2.X:

client_socket.send( "".join( [chr(c) for c in formatted_bytes] )

其他一些注释:


  • 您链接的代码是在消息长度上加1,这是不正确的。

  • The code you linked to is adding 1 to the message length which is incorrect.

您应该将握手代码与正常的消息解析代码分开。如果有人碰巧发送包含Sec-WebSocket-Key:的消息,您最终会通过发送一个消息来破坏流原始握手再次回来。

You should really have the handshake code separate from the normal message parsing code. If somebody happened to send a message which contained "Sec-WebSocket-Key: " you would end up corrupting the stream by sending a raw handshake back again.

这篇关于向websocket客户端发送消息时,压缩位必须为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆