admin管理员组文章数量:1401665
I'm very confused. I'm trying to understand how Websockets work and have build a server in node.js and a client side. What I couldn't understand is, how Websocket sends data. Is the data base64 encoded before sending from client (browser) ? How is 'binarytype' working ? Only for receiving ?
And do I have to implement these on server, too ? Do I need a 'binarytype' option on server side ?
When do I need blob and arraybuffer ? Depending on small and big data ? What is if get get videos and audio files ?
I had a look on how pictures should be sent on web and I found very often the solution, that the picture is base64 encoded and send after that. But if Websocket API in Browser already encode binary files to base64, I wouldn't have to do this..
Sorry, I'm very confused and hope somebody can help me
I'm very confused. I'm trying to understand how Websockets work and have build a server in node.js and a client side. What I couldn't understand is, how Websocket sends data. Is the data base64 encoded before sending from client (browser) ? How is 'binarytype' working ? Only for receiving ?
And do I have to implement these on server, too ? Do I need a 'binarytype' option on server side ?
When do I need blob and arraybuffer ? Depending on small and big data ? What is if get get videos and audio files ?
I had a look on how pictures should be sent on web and I found very often the solution, that the picture is base64 encoded and send after that. But if Websocket API in Browser already encode binary files to base64, I wouldn't have to do this..
Sorry, I'm very confused and hope somebody can help me
Share Improve this question asked Jul 3, 2015 at 20:31 benubenu 591 silver badge12 bronze badges2 Answers
Reset to default 4Read RFC 6455, especially 5. Data Framing, and you can find answers there.
In the WebSocket Protocol, data is transmitted using a sequence of frames (and not base64-encoded). Frame types defined in RFC 6455 are as follows.
- Continuation Frame (opcode = 0x0)
- Text Frame (opcode = 0x1)
- Binary Frame (opcode = 0x2)
- Close Frame (opcode = 0x8)
- Ping Frame (opcode = 0x9)
- Pong Frame (opcode = 0xA)
Decent WebSocket libraries (both client-side ones and server-side ones) can handle all of these frames and provide functions/methods to send/receive them. So, you don't have to worry about how data are encoded regardless of whether data are text or binary (unless you choice a bad WebSocket library).
Note that both clients and servers can send/receive both text frames and binary frames.
Websockets can transmit text and binary data. WebSocket binary data es in two shapes: blobs and array buffers.
ws.binaryType = "blob";
// or
ws.binaryType = "arraybuffer";
So you don't need to transform binary to text and text to binary to do transmission. You can use binary directly.
The problems you are probably referring to (e.g binary to base64) arose out of the fact that for a long time browsers didn't handle binary well in bination with JavaScript so you needed to shuffle text around instead, to bypass the limitations. This has improved in modern browsers (although I don't know the exact support in each major browser).
Have a look at this for more details: Processing Binary Protocols with Client-Side JavaScript
Regarding the server side, yes, you also need to handle binary data. If the client is expecting binary the server should send binary. Now, I'm not all that familiar with Node.js but a quick search turns up this package that looks promising: http://binaryjs./
本文标签: javascriptWebsocket base64binarytypeStack Overflow
版权声明:本文标题:javascript - Websocket base64, binarytype - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744211225a2595426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论