Sha256: 275aab97e5aa1aad21a5034f159a62b7a6b2a8506df523b65cc8119d0e98f3c9

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

require 'websocket/driver'
require 'socket'
require 'forwardable'
require 'celluloid/current'
require 'celluloid/io'

module Slack
  module RealTime
    module Concurrency
      module Celluloid
        class Socket < Slack::RealTime::Socket
          include ::Celluloid::IO
          include ::Celluloid::Internals::Logger

          BLOCK_SIZE = 4096

          extend ::Forwardable
          def_delegator :socket, :write
          def_delegators :driver, :text, :binary, :close

          attr_reader :socket

          def initialize(*args)
            super
            @driver = build_driver
          end

          # @yieldparam [WebSocket::Driver] driver
          def connect!
            super

            driver.start
            future.run_loop
          end

          def run_loop
            loop { read } if socket
          rescue EOFError
            # connection closed
          end

          def read
            buffer = socket.readpartial(BLOCK_SIZE)
            driver.parse buffer
          end

          def start_async
            future = yield self if block_given?

            Actor.new(future)
          end

          def connected?
            !@connected.nil?
          end

          protected

          class Actor
            attr_reader :future

            def initialize(future)
              @future = future
            end

            def join
              @future.value
            end
          end

          def build_socket
            socket = TCPSocket.new(addr, port)
            socket = SSLSocket.new(socket, build_ssl_context) if secure?
            socket
          end

          def build_ssl_context
            OpenSSL::SSL::SSLContext.new(:TLSv1_2_client)
          end

          def build_driver
            ::WebSocket::Driver.client(self)
          end

          def connect
            @socket = build_socket
            @connected = @socket.connect
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
slack-ruby-client-bhe-0.5.5 lib/slack/real_time/concurrency/celluloid.rb
slack-ruby-client-bhe-0.5.4 lib/slack/real_time/concurrency/celluloid.rb
slack-ruby-client-0.6.0 lib/slack/real_time/concurrency/celluloid.rb
slack-ruby-client-0.5.4 lib/slack/real_time/concurrency/celluloid.rb