Protocol::WebSocketSourceProtocolWebSocketMessage

class Message

Represents a message that can be sent or received over a WebSocket connection.

Definitions

def initialize(buffer = "")

Create a new message from a buffer.

Implementation

def initialize(buffer = "")
	@buffer = buffer
end

attr :buffer

Signature

attribute String

The message buffer.

def size

Signature

returns Integer

The size of the message buffer.

Implementation

def size
	@buffer.bytesize
end

def ==(other)

Compare this message to another message or buffer.

Implementation

def == other
	@buffer == other.to_str
end

def to_str

A message is implicitly convertible to it's buffer.

Implementation

def to_str
	@buffer
end

def encoding

The encoding of the message buffer.

Signature

returns Encoding

Implementation

def encoding
	@buffer.encoding
end

def self.generate(value, coder = Coder::DEFAULT)

Generate a message from a value using the given coder.

Implementation

def self.generate(value, coder = Coder::DEFAULT)
	new(coder.generate(value))
end

def parse(coder = Coder::DEFAULT)

Parse the message buffer using the given coder. Defaults to JSON.

Implementation

def parse(coder = Coder::DEFAULT)
	coder.parse(@buffer)
end

def to_h(...)

Convert the message buffer to a hash using the given coder. Defaults to JSON.

Implementation

def to_h(...)
	parse(...).to_h
end

def send(connection, **options)

Send this message as a text frame over the given connection.

Signature

parameter connection Connection

The WebSocket connection to send through.

Implementation

def send(connection, **options)
	connection.send_text(@buffer, **options)
end