Class: OnyxCord::Internal::WebSocket
- Inherits:
-
Object
- Object
- OnyxCord::Internal::WebSocket
- Defined in:
- lib/onyxcord/internal/websocket.rb
Instance Attribute Summary collapse
-
#connected ⇒ Object
(also: #connected?)
readonly
Returns the value of attribute connected.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(host, open_handler, message_handler, error_handler, close_handler) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #send(data) ⇒ Object
Constructor Details
#initialize(host, open_handler, message_handler, error_handler, close_handler) ⇒ WebSocket
Returns a new instance of WebSocket.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/onyxcord/internal/websocket.rb', line 15 def initialize(host, open_handler, , error_handler, close_handler) @host = host @open_handler = open_handler @message_handler = @error_handler = error_handler @close_handler = close_handler @connection = nil @connected = false connect end |
Instance Attribute Details
#connected ⇒ Object (readonly) Also known as: connected?
Returns the value of attribute connected.
11 12 13 |
# File 'lib/onyxcord/internal/websocket.rb', line 11 def connected @connected end |
Instance Method Details
#close ⇒ Object
38 39 40 41 42 43 |
# File 'lib/onyxcord/internal/websocket.rb', line 38 def close @connected = false @connection&.close rescue StandardError # Ignore errors on close end |
#send(data) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/onyxcord/internal/websocket.rb', line 28 def send(data) return unless @connection data = data.to_json if data.is_a?(Hash) @connection.write(Protocol::WebSocket::TextMessage.generate(data)) @connection.flush rescue StandardError => e @error_handler&.call(e) end |