class Slack::RealTime::Concurrency::Async::Socket

Attributes

client[R]

Public Instance Methods

close() click to toggle source

Close the socket.

Calls superclass method Slack::RealTime::Socket#close
# File lib/slack/real_time/concurrency/async.rb, line 92
def close
  super
ensure
  if @socket
    @socket.close
    @socket = nil
  end
end
connect!() click to toggle source
Calls superclass method Slack::RealTime::Socket#connect!
# File lib/slack/real_time/concurrency/async.rb, line 76
def connect!
  super
  run_loop
end
current_time() click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 72
def current_time
  ::Async::Clock.now
end
disconnect!() click to toggle source

Kill the restart/ping loop.

Calls superclass method Slack::RealTime::Socket#disconnect!
# File lib/slack/real_time/concurrency/async.rb, line 82
def disconnect!
  super
ensure
  if (restart = @restart)
    @restart = nil
    restart.signal
  end
end
restart_async(_client, new_url) click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 65
def restart_async(_client, new_url)
  @url = new_url
  @last_message_at = current_time

  @restart&.signal
end
run_loop() click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 101
def run_loop
  while @driver&.next_event
    # $stderr.puts event.inspect
  end
end
start_async(client) click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 22
def start_async(client)
  Thread.new do
    start_reactor(client)
  end
end
start_reactor(client) click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 28
def start_reactor(client)
  Async do |task|
    @restart = ::Async::Notification.new

    if client.run_ping?
      @ping_task = task.async do |subtask|
        subtask.annotate "#{client} keep-alive"

        # The timer task will naturally exit after the driver is set to nil.
        while @restart
          subtask.sleep client.websocket_ping_timer
          client.run_ping! if @restart
        end
      end
    end

    while @restart
      @client_task&.stop

      @client_task = task.async do |subtask|
        begin
          subtask.annotate "#{client} run-loop"
          client.run_loop
        rescue ::Async::Wrapper::Cancelled => e
          # Will get restarted by ping worker.
        rescue StandardError => e
          client.logger.error(subtask.to_s) { e.message }
        end
      end

      @restart.wait
    end

    @ping_task&.stop
  end
end
start_sync(client) click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 18
def start_sync(client)
  start_reactor(client).wait
end

Protected Instance Methods

build_endpoint() click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 115
def build_endpoint
  endpoint = ::Async::IO::Endpoint.tcp(addr, port)
  endpoint = ::Async::IO::SSLEndpoint.new(endpoint, ssl_context: build_ssl_context) if secure?
  endpoint
end
build_ssl_context() click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 109
def build_ssl_context
  OpenSSL::SSL::SSLContext.new(:TLSv1_2_client).tap do |ctx|
    ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER)
  end
end
connect() click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 125
def connect
  @socket = connect_socket
  @driver = Client.new(@socket, url)
end
connect_socket() click to toggle source
# File lib/slack/real_time/concurrency/async.rb, line 121
def connect_socket
  build_endpoint.connect
end