abstract class Amber::WebSockets::Decoders::Decoder

Overview

Abstract base class for WebSocket message decoders.

Decoders handle the serialization and deserialization of messages sent over WebSocket connections. The framework ships with JSON, text, and binary decoders, but custom decoders can be created by inheriting from this class.

Example:

class CustomDecoder < Amber::WebSockets::Decoders::Decoder
  def decode(raw : String) : JSON::Any
    # custom decoding logic
  end

  def encode(payload) : String
    # custom encoding logic
  end

  def content_type : String
    "application/x-custom"
  end
end

Direct Known Subclasses

Defined in:

amber/websockets/decoders/decoder.cr

Instance Method Summary

Instance Method Detail

abstract def content_type : String #

Returns the MIME content type associated with this decoder.


[View source]
abstract def decode(raw : String) : JSON::Any #

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]
abstract def encode(payload : Hash) : String #

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


[View source]
abstract def encode(payload : JSON::Any) : String #

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


[View source]