Class: Slack::RealTime::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/real_time/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Socket


10
11
12
13
14
15
16
# File 'lib/slack/real_time/socket.rb', line 10

def initialize(url, options = {})
  @url = url
  @options = options
  @driver = nil
  @logger = options.delete(:logger) || Slack::RealTime::Config.logger || Slack::Config.logger
  @last_message_at = nil
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver


6
7
8
# File 'lib/slack/real_time/socket.rb', line 6

def driver
  @driver
end

#optionsObject

Returns the value of attribute options


5
6
7
# File 'lib/slack/real_time/socket.rb', line 5

def options
  @options
end

#urlObject

Returns the value of attribute url


4
5
6
# File 'lib/slack/real_time/socket.rb', line 4

def url
  @url
end

Instance Method Details

#closeObject


78
79
80
81
82
83
84
# File 'lib/slack/real_time/socket.rb', line 78

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`.
  if driver = @driver
    @driver = nil
    driver.emit(:close)
  end
end

#connect! {|@driver| ... } ⇒ Object

Yields:


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/slack/real_time/socket.rb', line 28

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?Boolean


48
49
50
# File 'lib/slack/real_time/socket.rb', line 48

def connected?
  !@driver.nil?
end

#current_timeObject


74
75
76
# File 'lib/slack/real_time/socket.rb', line 74

def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#disconnect!Object

Gracefully shut down the connection.


42
43
44
45
46
# File 'lib/slack/real_time/socket.rb', line 42

def disconnect!
  @driver.close
ensure
  close
end

#restart_async(_client, _url) ⇒ Object

Raises:

  • (NotImplementedError)

64
65
66
# File 'lib/slack/real_time/socket.rb', line 64

def restart_async(_client, _url)
  raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end

#send_data(message) ⇒ Object


18
19
20
21
22
23
24
25
26
# File 'lib/slack/real_time/socket.rb', line 18

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) ⇒ #join

Raises:

  • (NotImplementedError)

60
61
62
# File 'lib/slack/real_time/socket.rb', line 60

def start_async(_client)
  raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end

#start_sync(client) ⇒ Object


52
53
54
55
56
57
# File 'lib/slack/real_time/socket.rb', line 52

def start_sync(client)
  thread = start_async(client)
  thread.join if thread
rescue Interrupt
  thread.exit if thread
end

#time_since_last_messageObject


68
69
70
71
72
# File 'lib/slack/real_time/socket.rb', line 68

def time_since_last_message
  return 0 unless @last_message_at

  current_time - @last_message_at
end