Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failed: Could not decode a text frame as UTF-8. #33

Open
ants-sinica opened this issue Sep 6, 2017 · 1 comment
Open

failed: Could not decode a text frame as UTF-8. #33

ants-sinica opened this issue Sep 6, 2017 · 1 comment

Comments

@ants-sinica
Copy link

I have program running perfectly with txWS and Python2.7 via SSL channel. Recently the program upgraded to Python 3.6 and pip-installed a new copy of Twisted and txWS. Sending message to server works fine. But the other direction goes something wrong: The error message is
"failed: Could not decode a text frame as UTF-8." in Chrome when the payload size is over 100 bytes. Is there any suggestion?

@iapyeh
Copy link

iapyeh commented Jul 25, 2018

The following code works for me: (because pack in python3 returns differently)

def make_hybi07_frame(buf, opcode=0x1):
    """
    Make a HyBi-07 frame.

    This function always creates unmasked frames, and attempts to use the
    smallest possible lengths.
    """
    if isinstance(buf, six.text_type):
        buf = buf.encode('utf-8')

    _len = len(buf)
    if _len <= 125:
        length = six.b(chr(_len))
    elif _len <= 65535:
        length = b"\x7e" + pack('>H',_len)
    else:
        length = b"\x7f" + pack('>Q',_len)


    # Always make a normal packet.
    header = chr(0x80 | opcode)
    return six.b(header) + length + buf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants