Class: Protocol::WebSocket::Message
- Inherits:
-
Object
- Object
- Protocol::WebSocket::Message
- Defined in:
- lib/protocol/websocket/message.rb
Overview
Represents a message that can be sent or received over a WebSocket connection.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
- #The message buffer.(messagebuffer.) ⇒ Object readonly
Class Method Summary collapse
-
.generate(value, coder = Coder::DEFAULT) ⇒ Object
Generate a message from a value using the given coder.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare this message to another message or buffer.
-
#encoding ⇒ Object
The encoding of the message buffer.
-
#initialize(buffer = "") ⇒ Message
constructor
Create a new message from a buffer.
-
#parse(coder = Coder::DEFAULT) ⇒ Object
Parse the message buffer using the given coder.
- #send(connection, **options) ⇒ Object
- #size ⇒ Object
-
#to_h ⇒ Object
Convert the message buffer to a hash using the given coder.
-
#to_str ⇒ Object
A message is implicitly convertible to it’s buffer.
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
#buffer ⇒ Object (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 |
#encoding ⇒ Object
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, **) connection.send_text(@buffer, **) end |
#size ⇒ Object
23 24 25 |
# File 'lib/protocol/websocket/message.rb', line 23 def size @buffer.bytesize end |
#to_h ⇒ Object
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_str ⇒ Object
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 |