class Slack::RealTime::Socket

Attributes

driver[R]
logger[R]
options[RW]
url[RW]

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/slack/real_time/socket.rb, line 9
def initialize(url, options = {})
  @url = url
  @options = options.dup
  @driver = nil
  @logger = @options.delete(:logger) || Slack::RealTime::Config.logger || Slack::Config.logger
  @last_message_at = nil
end

Public Instance Methods

close() click to toggle source
# File lib/slack/real_time/socket.rb, line 77
def close
  # When you call `driver.emit(:close)`, it will typically end up calling `client.close`
  # which will call `@socket.close` and end up back here. In order to break this infinite
  # recursion, we check and set `@driver = nil` before invoking `client.close`.
  return unless (driver = @driver)

  @driver = nil
  driver.emit(:close)
end
connect!() { |driver| ... } click to toggle source
# File lib/slack/real_time/socket.rb, line 27
def connect!
  return if connected?

  connect
  logger.debug("#{self.class}##{__method__}") { @driver.class }

  @driver.on :message do
    @last_message_at = current_time
  end

  yield @driver if block_given?
end
connected?() click to toggle source
# File lib/slack/real_time/socket.rb, line 47
def connected?
  !@driver.nil?
end
current_time() click to toggle source
# File lib/slack/real_time/socket.rb, line 73
def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
disconnect!() click to toggle source

Gracefully shut down the connection.

# File lib/slack/real_time/socket.rb, line 41
def disconnect!
  @driver.close
ensure
  close
end
restart_async(_client, _url) click to toggle source
# File lib/slack/real_time/socket.rb, line 63
def restart_async(_client, _url)
  raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end
send_data(message) click to toggle source
# File lib/slack/real_time/socket.rb, line 17
def send_data(message)
  logger.debug("#{self.class}##{__method__}") { message }
  case message
  when Numeric then @driver.text(message.to_s)
  when String  then @driver.text(message)
  when Array   then @driver.binary(message)
  else false
  end
end
start_async(_client) click to toggle source

@return [#join]

# File lib/slack/real_time/socket.rb, line 59
def start_async(_client)
  raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end
start_sync(client) click to toggle source
# File lib/slack/real_time/socket.rb, line 51
def start_sync(client)
  thread = start_async(client)
  thread&.join
rescue Interrupt
  thread&.exit
end
time_since_last_message() click to toggle source
# File lib/slack/real_time/socket.rb, line 67
def time_since_last_message
  return 0 unless @last_message_at

  current_time - @last_message_at
end

Protected Instance Methods

addr() click to toggle source
# File lib/slack/real_time/socket.rb, line 91
def addr
  URI(url).host
end
connect() click to toggle source
# File lib/slack/real_time/socket.rb, line 110
def connect
  raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end
port() click to toggle source
# File lib/slack/real_time/socket.rb, line 99
def port
  case (uri = URI(url)).scheme
  when 'wss', 'https'
    URI::HTTPS::DEFAULT_PORT
  when 'ws', 'http'
    URI::HTTP::DEFAULT_PORT
  else
    uri.port
  end
end
secure?() click to toggle source
# File lib/slack/real_time/socket.rb, line 95
def secure?
  port == URI::HTTPS::DEFAULT_PORT
end