Class: Caldera::WebSocket
- Inherits:
-
Object
- Object
- Caldera::WebSocket
- Includes:
- EventEmitter
- Defined in:
- lib/caldera/websocket.rb
Constant Summary collapse
- LOGGER =
Logging.logger[self]
Instance Attribute Summary collapse
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#close(message: nil, code: 1000) ⇒ Object
Send a close frame.
-
#initialize(uri, authorization, num_shards, user_id) ⇒ WebSocket
constructor
Create a WebSocket that connects to the Lavalink server.
-
#send_json(message) ⇒ Object
Encode a hash to json, and send it over the websocket.
-
#start ⇒ Object
Start the connection to the Lavalink server.
- #url ⇒ Object
-
#write(data) ⇒ Object
Write data to the socket.
Constructor Details
#initialize(uri, authorization, num_shards, user_id) ⇒ WebSocket
Create a WebSocket that connects to the Lavalink server.
25 26 27 28 29 30 31 32 |
# File 'lib/caldera/websocket.rb', line 25 def initialize(uri, , num_shards, user_id) @uri = uri.is_a?(URI::Generic) ? uri : URI.parse(uri) @uri.scheme = 'ws' create_driver set_headers(, num_shards, user_id) register_handlers end |
Instance Attribute Details
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
15 16 17 |
# File 'lib/caldera/websocket.rb', line 15 def thread @thread end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
15 16 17 |
# File 'lib/caldera/websocket.rb', line 15 def uri @uri end |
Instance Method Details
#close(message: nil, code: 1000) ⇒ Object
Send a close frame
61 62 63 64 |
# File 'lib/caldera/websocket.rb', line 61 def close(message: nil, code: 1000) LOGGER.debug { "Sending close: (#{.inspect}, #{code})" } @driver.close(reason, code) end |
#send_json(message) ⇒ Object
Encode a hash to json, and send it over the websocket.
47 48 49 50 |
# File 'lib/caldera/websocket.rb', line 47 def send_json() LOGGER.debug { "Sending message: #{.inspect}" } @driver.text(JSON.dump()) end |
#start ⇒ Object
Start the connection to the Lavalink server.
35 36 37 38 39 40 41 42 43 |
# File 'lib/caldera/websocket.rb', line 35 def start LOGGER.info { "Opening connection to #{url}" } @tcp = TCPSocket.new(@uri.host || localhost, @uri.port) @dead = false create_thread @driver.start LOGGER.info { 'Driver started' } end |
#url ⇒ Object
66 67 68 |
# File 'lib/caldera/websocket.rb', line 66 def url @uri.to_s end |
#write(data) ⇒ Object
Write data to the socket
54 55 56 |
# File 'lib/caldera/websocket.rb', line 54 def write(data) @tcp.write(data) end |