Streaming binary and strings

I am experimenting a bit with WebSockets and Java. According to the latest WebSocket project, a message can be a binary or a simple string. I use a Webbit server and has two functions:

public void onMessage(WebSocketConnection connection, String message) public void onMessage(WebSocketConnection connection, byte[] message) 

I wonder what the difference is. Is byte [] fast? Or why does it matter? I can write everything that I write with bytes, because even a string consists of bytes during transmission, so why do we have two different methods? Only Google Chrome 15 Beta and 16 Dev supports binary transfer, so I thought about using Base64 encoding / decoding on both the client and server. Is this the only difference? What if I just read every byte, put them in a string and send them? I think the only difference will be that not all bytes are string characters, so will I just add the overhead when converting to a string?

tl; dr → What is the difference between binary wrapping and line wrapping?

+4
source share
2 answers

The WebSocket protocol (HyBi) supports two different types of payload: text, binary. Text payload is UTF-8 encoded string data. Any ASCII codes above 127 in the sent string will be converted to double-byte UTF-8 encoding. To successfully send / receive raw binary data, you probably want to encode the data in much the same way as base64 (which is compatible with UTF-8).

The binary payload type is sent directly. Bytes are sent as is in the payload. This increases throughput. This means that you do not need to do the encoding / decoding step. The bytes you send are sent directly, and the bytes you receive can be accessed directly without decoding.

+6
source

When you use bytes, it actually just converts it to a hex file before sending it. As for the speed. I do not think it matters. It seems like they give you too many options, so you can send this data anyway, depending on what you already have for the data.

0
source

All Articles