class Caldera::WebSocket

Constants

LOGGER

Attributes

thread[R]
uri[R]

Public Class Methods

new(uri, authorization, num_shards, user_id) click to toggle source

Create a WebSocket that connects to the Lavalink server. @param [URI, String] uri The URI to connect with. (ex: `“ws://localhost:8080”`) @param [String] authorization Password matching the server config. @param [Integer] num_shards Total number of shards your bot is operating on. @param [String, Integer] user_id The user ID of the bot you are playing music with. @caldera.lavalink_docs github.com/Frederikam/Lavalink/blob/master/IMPLEMENTATION.md#opening-a-connection

# File lib/caldera/websocket.rb, line 25
def initialize(uri, authorization, num_shards, user_id)
  @uri = uri.is_a?(URI::Generic) ? uri : URI.parse(uri)
  @uri.scheme = 'ws'

  create_driver
  set_headers(authorization, num_shards, user_id)
  register_handlers
end

Public Instance Methods

close(message: nil, code: 1000) click to toggle source

Send a close frame @param [String] message The close reason @param [Integer] code The close code

# File lib/caldera/websocket.rb, line 61
def close(message: nil, code: 1000)
  LOGGER.debug { "Sending close: (#{message.inspect}, #{code})" }
  @driver.close(reason, code)
end
send_json(message) click to toggle source

Encode a hash to json, and send it over the websocket. @param [Hash] message

# File lib/caldera/websocket.rb, line 47
def send_json(message)
  LOGGER.debug { "Sending message: #{message.inspect}" }
  @driver.text(JSON.dump(message))
end
start() click to toggle source

Start the connection to the Lavalink server.

# 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() click to toggle source
# File lib/caldera/websocket.rb, line 66
def url
  @uri.to_s
end
write(data) click to toggle source

Write data to the socket @param [String] data

# File lib/caldera/websocket.rb, line 54
def write(data)
  @tcp.write(data)
end

Private Instance Methods

create_driver() click to toggle source

Construct a WebSocket::Driver instance.

# File lib/caldera/websocket.rb, line 73
def create_driver
  @driver = ::WebSocket::Driver.client(self)
  @driver.add_extension(PermessageDeflate)
end
create_thread() click to toggle source

Begin the thread that feeds messages to the driver.

# File lib/caldera/websocket.rb, line 99
def create_thread
  LOGGER.debug { 'Creating read thread' }
  @thread = Thread.new do
    @driver.parse(read_data) until @dead
    LOGGER.debug { 'Read loop ending' }
  end
end
handle_close(event) click to toggle source

Fired when the WS connection has sent a close frame.

# File lib/caldera/websocket.rb, line 121
def handle_close(event)
  LOGGER.warn { "Received a close frame: (#{event.reason}, #{event.code})" }
  @dead = true
  @thread.kill
  emit(:close, { reason: event.reason, code: event.code })
end
handle_message(event) click to toggle source

Fired when a message frame has been received

# File lib/caldera/websocket.rb, line 114
def handle_message(event)
  LOGGER.info("Received message: #{event.data.inspect}")
  parsed = JSON.load(event.data)
  emit(parsed['op'], parsed)
end
handle_open(_event) click to toggle source

Fired when the WS handshake has finished

# File lib/caldera/websocket.rb, line 108
def handle_open(_event)
  LOGGER.info('WebSocket connected')
  emit(:open)
end
read_data(length = 4096) click to toggle source

Read data from the TCP socket. @param [Integer] length The maximum length to read at once.

# File lib/caldera/websocket.rb, line 130
def read_data(length = 4096)
  @tcp.readpartial(length)
end
register_handlers() click to toggle source

Register event handlers on the driver.

# File lib/caldera/websocket.rb, line 91
def register_handlers
  LOGGER.debug { 'Registering driver handlers' }
  @driver.on(:open, &method(:handle_open))
  @driver.on(:message, &method(:handle_message))
  @driver.on(:close, &method(:handle_close))
end
set_headers(authorization, num_shards, user_id) click to toggle source

Set the relevant headers for opening a connection. @param [String] authorization @param [Integer] num_shards @param [Integer, String] user_id @caldera.lavalink_docs [Opening a connection](github.com/Frederikam/Lavalink/blob/master/IMPLEMENTATION.md#opening-a-connection)

# File lib/caldera/websocket.rb, line 83
def set_headers(authorization, num_shards, user_id)
  LOGGER.debug { 'Setting connection headers' }
  @driver.set_header('Authorization', authorization)
  @driver.set_header('Num-Shards', num_shards)
  @driver.set_header('User-Id', user_id)
end