admin管理员组文章数量:1293566
I am trying to use websockets in binary mode to transfer binary data from a server written in Python to a client browser (running javascript). I have implemented this munication in text mode, but now I wish to boost the performance by municating in binary mode instead.
All the examples I have seen (eg this, and also if one dig through the source code of matplotlib/webagg which uses Tornado) claim they are using binary mode, but in fact they seem to encode the raw binary data into UTF-8 (or base64) at some point before transmission. In my view doing this is not true binary transfer as it adds 30% to 50% overhead.
So my question is, do one necessarily have to encode the binary data into utf-8 or base64 in order to use IP based websockets? If not, please point me to an example where this is done without encoding.
I have always believed that sockets do support true binary munication but maybe this is not the case for IP websockets for some reason. Perhaps someone can shed light onto the matter. There seem to have been advances in the past year or so in this area so that adds to the confusion.
I am trying to use websockets in binary mode to transfer binary data from a server written in Python to a client browser (running javascript). I have implemented this munication in text mode, but now I wish to boost the performance by municating in binary mode instead.
All the examples I have seen (eg this, and also if one dig through the source code of matplotlib/webagg which uses Tornado) claim they are using binary mode, but in fact they seem to encode the raw binary data into UTF-8 (or base64) at some point before transmission. In my view doing this is not true binary transfer as it adds 30% to 50% overhead.
So my question is, do one necessarily have to encode the binary data into utf-8 or base64 in order to use IP based websockets? If not, please point me to an example where this is done without encoding.
I have always believed that sockets do support true binary munication but maybe this is not the case for IP websockets for some reason. Perhaps someone can shed light onto the matter. There seem to have been advances in the past year or so in this area so that adds to the confusion.
Share Improve this question edited Feb 11, 2013 at 14:20 Jules 14.5k13 gold badges59 silver badges106 bronze badges asked Feb 11, 2013 at 13:56 mattmatt 911 silver badge2 bronze badges 1- You may find this to be of relevance. – Tomasz Łazarowicz Commented Feb 11, 2013 at 15:57
1 Answer
Reset to default 11The IETF 6455 WebSocket Protocol supports direct send/receive of binary data (the older Hixie protocol variant did not). If you are implementing your own framing, then you just need to set the opcode in the frame to 0x2 to indicate that the payload is raw binary data rather than UTF-8 encoded text. If you are using a python WebSocket library then you need to use the API provided by that library to select binary mode (if the library supports it).
Note: the example you linked to is not a WebSocket example (it is just a regular TCP client and server). Also, that example is not encoding the data as UTF-8 or base64. Websockify is a WebSocket server that supports direct binary data (in addition to base64 encoding for the older Hixie variant). Disclaimer: I created websockify.
本文标签: javascriptBinary data over websocket without encoding to UTF8 or base64Stack Overflow
版权声明:本文标题:javascript - Binary data over websocket without encoding to UTF-8 or base64 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741568559a2385867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论