Class: Protocol::WebSocket::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/websocket/message.rb

Overview

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

Direct Known Subclasses

BinaryMessage, PingMessage, TextMessage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer = "") ⇒ Message

Create a new message from a buffer.



15
16
17
# File 'lib/protocol/websocket/message.rb', line 15

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



15
16
17
# File 'lib/protocol/websocket/message.rb', line 15

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

#The message buffer.(messagebuffer.) ⇒ Object (readonly)



20
# File 'lib/protocol/websocket/message.rb', line 20

attr :buffer

Class Method Details

.generate(value, coder = Coder::DEFAULT) ⇒ Object

Generate a message from a value using the given coder.



46
47
48
# File 'lib/protocol/websocket/message.rb', line 46

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

Instance Method Details

#==(other) ⇒ Object

Compare this message to another message or buffer.



28
29
30
# File 'lib/protocol/websocket/message.rb', line 28

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

#encodingObject

The encoding of the message buffer.



39
40
41
# File 'lib/protocol/websocket/message.rb', line 39

def encoding
  @buffer.encoding
end

#parse(coder = Coder::DEFAULT) ⇒ Object

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



51
52
53
# File 'lib/protocol/websocket/message.rb', line 51

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

#send(connection, **options) ⇒ Object



60
61
62
# File 'lib/protocol/websocket/message.rb', line 60

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

#sizeObject



23
24
25
# File 'lib/protocol/websocket/message.rb', line 23

def size
  @buffer.bytesize
end

#to_hObject

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



56
57
58
# File 'lib/protocol/websocket/message.rb', line 56

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

#to_strObject

A message is implicitly convertible to it’s buffer.



33
34
35
# File 'lib/protocol/websocket/message.rb', line 33

def to_str
  @buffer
end