Class: Slack::RealTime::Socket
- Inherits:
-
Object
- Object
- Slack::RealTime::Socket
show all
- Defined in:
- lib/slack/real_time/socket.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(url, options = {}) ⇒ Socket
Returns a new instance of Socket.
8
9
10
11
12
13
14
|
# File 'lib/slack/real_time/socket.rb', line 8
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
|
Instance Attribute Details
#driver ⇒ Object
Returns the value of attribute driver.
6
7
8
|
# File 'lib/slack/real_time/socket.rb', line 6
def driver
@driver
end
|
#options ⇒ Object
Returns the value of attribute options.
5
6
7
|
# File 'lib/slack/real_time/socket.rb', line 5
def options
@options
end
|
#url ⇒ Object
Returns the value of attribute url.
5
6
7
|
# File 'lib/slack/real_time/socket.rb', line 5
def url
@url
end
|
Instance Method Details
#close ⇒ Object
80
81
82
83
84
85
86
87
88
|
# File 'lib/slack/real_time/socket.rb', line 80
def close
return unless (driver = @driver)
@driver = nil
driver.emit(:close)
end
|
#connect! {|@driver| ... } ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/slack/real_time/socket.rb', line 26
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
46
47
48
|
# File 'lib/slack/real_time/socket.rb', line 46
def connected?
!@driver.nil?
end
|
#current_time ⇒ Object
76
77
78
|
# File 'lib/slack/real_time/socket.rb', line 76
def current_time
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
|
#disconnect! ⇒ Object
Gracefully shut down the connection.
40
41
42
43
44
|
# File 'lib/slack/real_time/socket.rb', line 40
def disconnect!
@driver.close
ensure
close
end
|
#restart_async(_client, _url) ⇒ Object
62
63
64
|
# File 'lib/slack/real_time/socket.rb', line 62
def restart_async(_client, _url)
raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end
|
#run_async(&_block) ⇒ Object
66
67
68
|
# File 'lib/slack/real_time/socket.rb', line 66
def run_async(&_block)
raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end
|
#send_data(message) ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/slack/real_time/socket.rb', line 16
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
58
59
60
|
# File 'lib/slack/real_time/socket.rb', line 58
def start_async(_client)
raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end
|
#start_sync(client) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/slack/real_time/socket.rb', line 50
def start_sync(client)
thread = start_async(client)
thread&.join
rescue Interrupt
thread&.exit
end
|
#time_since_last_message ⇒ Object
70
71
72
73
74
|
# File 'lib/slack/real_time/socket.rb', line 70
def time_since_last_message
return 0 unless @last_message_at
current_time - @last_message_at
end
|