Skip to content

Commit

Permalink
Moving reconnect logic into client
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodney Urquhart committed Oct 10, 2018
1 parent fc25efa commit 88ea7a5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 30 deletions.
31 changes: 20 additions & 11 deletions lib/slack/real_time/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,30 @@ def run_loop
end

def run_ping
return if websocket_ping.nil? || websocket_ping < 1
loop do
yield websocket_ping if block_given?
raise ClientNotStartedError unless started?

unless @socket.alive
@socket.disconnect!
@socket.close
return unless run_ping?
on(:pong) { @socket.pong_received = true }
begin
loop do
@socket.pong_received = false
ping

yield websocket_ping if block_given?

unless @socket.pong_received
@socket.disconnect!
@socket.close
end
end

ping
@socket.alive = false
rescue Slack::RealTime::Client::ClientNotStartedError
@socket.restart_async(self)
retry
end
end

def run_ping?
!websocket_ping.nil? && websocket_ping > 1
end

protected

# @return [Slack::RealTime::Socket]
Expand Down
19 changes: 9 additions & 10 deletions lib/slack/real_time/concurrency/async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@ def start_async(client)
client.run_loop
end
::Async::Reactor.run do |task|
begin
client.run_ping do |delay|
task.sleep delay
end
rescue Slack::RealTime::Client::ClientNotStartedError
::Async::Reactor.run do
client.build_socket
client.run_loop
end
retry
client.run_ping do |delay|
task.sleep delay
end
end
end
end
end

def restart_async(client)
::Async::Reactor.run do
client.build_socket
client.run_loop
end
end

def connect!
super
run_loop
Expand Down
15 changes: 6 additions & 9 deletions lib/slack/real_time/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module RealTime
class Socket
attr_accessor :url
attr_accessor :options
attr_accessor :alive
attr_accessor :pong_received
attr_reader :driver
attr_reader :logger
protected :logger
Expand All @@ -13,7 +13,7 @@ def initialize(url, options = {})
@options = options
@driver = nil
@logger = options.delete(:logger) || Slack::RealTime::Config.logger || Slack::Config.logger
@alive = false
@pong_received = false
end

def send_data(message)
Expand All @@ -32,13 +32,6 @@ def connect!
connect
logger.debug("#{self.class}##{__method__}") { driver.class }

@alive = true

driver.on :message do |event|
event_data = JSON.parse(event.data)
@alive = true if event_data['type'] == 'pong'
end

yield driver if block_given?
end

Expand All @@ -62,6 +55,10 @@ def start_async(_client)
raise NotImplementedError, "Expected #{self.class} to implement #{__method__}."
end

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

def close
@driver = nil
end
Expand Down

0 comments on commit 88ea7a5

Please sign in to comment.