class Amber::WebSockets::Decoders::BinaryDecoder

Overview

Binary decoder for WebSocket messages using a simple length-prefixed format.

This decoder provides a compact binary serialization format for WebSocket messages. Each field is encoded as a length-prefixed UTF-8 string:

[key_length:4 bytes][key_data][value_length:4 bytes][value_data]...

The field count is stored as the first 4 bytes. This format is suitable when bandwidth efficiency is important and both client and server support binary WebSocket frames.

Since Crystal WebSocket messages arrive as strings, the binary data is Base64-encoded for transport. If you receive raw binary frames, decode them from Base64 first.

Example:

decoder = Amber::WebSockets::Decoders::BinaryDecoder.new
encoded = decoder.encode({"event" => "message", "topic" => "room:1"})
decoded = decoder.decode(encoded)
decoded["event"].as_s # => "message"

Defined in:

amber/websockets/decoders/binary_decoder.cr

Instance Method Summary

Instance methods inherited from class Amber::WebSockets::Decoders::Decoder

content_type : String content_type, decode(raw : String) : JSON::Any decode, encode(payload : Hash) : String
encode(payload : JSON::Any) : String
encode

Instance Method Detail

def content_type : String #
Description copied from class Amber::WebSockets::Decoders::Decoder

Returns the MIME content type associated with this decoder.


[View source]
def decode(raw : String) : JSON::Any #
Description copied from class Amber::WebSockets::Decoders::Decoder

Decodes a raw message string into a structured JSON::Any value.

Returns a JSON::Any representing the decoded message. Implementations should raise DecoderError if the message cannot be decoded.


[View source]
def encode(payload : Hash) : String #
Description copied from class Amber::WebSockets::Decoders::Decoder

Encodes a Hash payload into a string suitable for sending over the socket.


[View source]
def encode(payload : JSON::Any) : String #
Description copied from class Amber::WebSockets::Decoders::Decoder

Encodes a JSON::Any payload into a string suitable for sending over the socket.


[View source]