lib/ruboty/slack_rtm/client.rb in ruboty-slack_rtm-2.4.4 vs lib/ruboty/slack_rtm/client.rb in ruboty-slack_rtm-2.5.0

- old
+ new

@@ -2,10 +2,12 @@ require 'websocket-client-simple' module Ruboty module SlackRTM class Client + CONNECTION_CLOSED = Object.new + def initialize(websocket_url:) @client = create_client(websocket_url.to_s) @queue = Queue.new end @@ -34,19 +36,26 @@ def main_loop keep_connection loop do message = @queue.deq + if message.equal?(CONNECTION_CLOSED) + break + end @client.send(message) end end private def create_client(url) WebSocket::Client::Simple.connect(url, verify_mode: OpenSSL::SSL::VERIFY_PEER).tap do |client| client.on(:error) do |err| Ruboty.logger.error("#{err.class}: #{err.message}\n#{err.backtrace.join("\n")}") + end + client.on(:close) do + Ruboty.logger.info('Disconnected') + @queue.enq(CONNECTION_CLOSED) end end end def keep_connection