class
Amber::WebSockets::Decoders::TextDecoder
Overview
Plain text decoder for WebSocket messages.
This decoder treats messages as plain text strings. It wraps the raw text in a JSON structure with an "event" field set to "message" and the raw text placed in the "payload" field, making it compatible with the channel dispatch system.
This is useful for simple text-based protocols or when clients send unstructured text messages.
Example:
decoder = Amber::WebSockets::Decoders::TextDecoder.new
decoded = decoder.decode("hello world")
decoded["event"].as_s # => "message"
decoded["payload"].as_s # => "hello world"
encoded = decoder.encode({"event" => "message", "payload" => "hello"})
# => "{\"event\":\"message\",\"payload\":\"hello\"}"
Defined in:
amber/websockets/decoders/text_decoder.crInstance Method Summary
-
#content_type : String
Returns the MIME content type associated with this decoder.
-
#decode(raw : String) : JSON::Any
Decodes a raw message string into a structured JSON::Any value.
-
#encode(payload : Hash) : String
Encodes a Hash payload into a string suitable for sending over the socket.
-
#encode(payload : JSON::Any) : String
Encodes a JSON::Any payload into a string suitable for sending over the socket.
Instance methods inherited from class Amber::WebSockets::Decoders::Decoder
content_type : String
content_type,
decode(raw : String) : JSON::Any
decode,
encode(payload : Hash) : Stringencode(payload : JSON::Any) : String encode
Instance Method Detail
Returns the MIME content type associated with this 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.
Encodes a Hash payload into a string suitable for sending over the socket.
Encodes a JSON::Any payload into a string suitable for sending over the socket.