lib/slack/real_time/client.rb in slack-ruby-client-0.13.1 vs lib/slack/real_time/client.rb in slack-ruby-client-0.14.0

- old
+ new

@@ -45,24 +45,25 @@ end # Start RealTime client and block until it disconnects. def start!(&block) @callback = block if block_given? - @socket = build_socket + build_socket @socket.start_sync(self) end # Start RealTime client and return immediately. # The RealTime::Client will run in the background. def start_async(&block) @callback = block if block_given? - @socket = build_socket + build_socket @socket.start_async(self) end def stop! raise ClientNotStartedError unless started? + @socket.disconnect! if @socket end def started? @socket && @socket.connected? @@ -100,20 +101,45 @@ callback(event, :closed) end end end + def run_ping! + time_since_last_message = @socket.time_since_last_message + return if time_since_last_message < websocket_ping + raise Slack::RealTime::Client::ClientNotStartedError if !@socket.connected? || time_since_last_message > (websocket_ping * 2) + + ping + rescue Slack::RealTime::Client::ClientNotStartedError + restart_async + end + + def run_ping? + !websocket_ping.nil? && websocket_ping > 0 + end + protected + def restart_async + logger.debug("#{self.class}##{__method__}") + @socket.close + start = web_client.send(rtm_start_method, start_options) + data = Slack::Messages::Message.new(start) + @url = data.url + @store = @store_class.new(data) if @store_class + @socket.restart_async(self, @url) + end + # @return [Slack::RealTime::Socket] def build_socket raise ClientAlreadyStartedError if started? + start = web_client.send(rtm_start_method, start_options) data = Slack::Messages::Message.new(start) @url = data.url @store = @store_class.new(data) if @store_class - socket_class.new(@url, socket_options) + @socket = socket_class.new(@url, socket_options) end def rtm_start_method if start_method start_method @@ -137,28 +163,27 @@ concurrency::Socket end def send_json(data) raise ClientNotStartedError unless started? + logger.debug("#{self.class}##{__method__}") { data } @socket.send_data(data.to_json) end def open(_event); end def close(_event) - socket = @socket - @socket = nil - - [socket, socket_class].each do |s| + [@socket, socket_class].each do |s| s.close if s.respond_to?(:close) end end def callback(event, type) callbacks = self.callbacks[type.to_s] return false unless callbacks + callbacks.each do |c| c.call(event) end true rescue StandardError => e @@ -166,13 +191,15 @@ false end def dispatch(event) return false unless event.data + data = Slack::Messages::Message.new(JSON.parse(event.data)) type = data.type return false unless type + type = type.to_s logger.debug("#{self.class}##{__method__}") { data.to_s } run_handlers(type, data) if @store run_callbacks(type, data) rescue StandardError => e @@ -193,9 +220,10 @@ end def run_callbacks(type, data) callbacks = self.callbacks[type] return false unless callbacks + callbacks.each do |c| c.call(data) end true rescue StandardError => e