Sha256: 20a72c461bc1cab2a0f07a667caade9a61d377429fab64d2d955e6d747cc0bdc

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require 'async/websocket'

module Slack
  module RealTime
    module Concurrency
      module Async
        class Client < ::Async::WebSocket::Client
          extend ::Forwardable
          def_delegators :@driver, :on, :text, :binary, :emit
        end

        class Socket < Slack::RealTime::Socket
          attr_reader :client

          def start_async(client)
            Thread.new do
              ::Async::Reactor.run do
                client.run_loop
              end
            end
          end

          def connect!
            super
            run_loop
          end

          def close
            @closing = true
            @driver.close if @driver
            super
          end

          def run_loop
            @closing = false
            while @driver && @driver.next_event
              # $stderr.puts event.inspect
            end
          end

          protected

          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

          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

          def connect_socket
            build_endpoint.connect
          end

          def connect
            @socket = connect_socket
            @driver = Client.new(@socket, url)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slack-ruby-client-0.13.1 lib/slack/real_time/concurrency/async.rb
slack-ruby-client-0.13.0 lib/slack/real_time/concurrency/async.rb