Pithikos / python-websocket-server

A simple fully working websocket-server in Python with no external dependencies
MIT License
1.14k stars 381 forks source link

how can i receive an chinese word? #72

Closed cuipy closed 3 years ago

cuipy commented 5 years ago

if client send any words with gbk charset,i receive is error words . you can test with this website: http://www.blue-zero.com/WebSocket/ it is pain of chinese user

Yhangzzz commented 5 years ago

websocket-server不支持中文,解决方法有两个:

  1. 修改库文件websocket_server.py中read_next_message函数以支持中文!! 效率高 decoded = bytearray() for char in self.read_bytes(payload_length): char ^= masks[len(decoded) % 4] decoded.append(char) decoded = str(decoded, encoding="utf-8")
  2. 在接收函数中再次处理,效率低 bytestream = bytearray() for ch in message: bytestream.append(ord(ch)) message = str(bytestream, encoding="utf-8")
Yhangzzz commented 5 years ago

主要因为库中解码过程中使用chr()对每个接收的字节处理,从而破坏了GBK编码的汉字,或者其他编码的多字节字符。使用str()则不会破坏。可以尝试下,我这边解决了

Pithikos commented 5 years ago

Feel free to make a PR if there is a known solution :+1:

Pithikos commented 3 years ago

Closing since I can't understand any of this. Feel free to post a solution in English and I can add it.