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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

Read 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.

  1. Continuation Frame (opcode = 0x0)
  2. Text Frame (opcode = 0x1)
  3. Binary Frame (opcode = 0x2)
  4. Close Frame (opcode = 0x8)
  5. Ping Frame (opcode = 0x9)
  6. 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