Sha256: d7b88105d711a05f31105c8bfde92f17269b68ba0c6093a395385a96a3f2bd45

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

# :markup: markdown

module ActionCable
  module Connection
    # # Action Cable InternalChannel
    #
    # Makes it possible for the RemoteConnection to disconnect a specific
    # connection.
    module InternalChannel
      extend ActiveSupport::Concern

      private
        def internal_channel
          "action_cable/#{connection_identifier}"
        end

        def subscribe_to_internal_channel
          if connection_identifier.present?
            callback = -> (message) { process_internal_message ActiveSupport::JSON.decode(message) }
            @_internal_subscriptions ||= []
            @_internal_subscriptions << [ internal_channel, callback ]

            pubsub.subscribe(internal_channel, callback)
            logger.info "Registered connection (#{connection_identifier})"
          end
        end

        def unsubscribe_from_internal_channel
          if @_internal_subscriptions.present?
            @_internal_subscriptions.each { |channel, callback| pubsub.unsubscribe(channel, callback) }
          end
        end

        def process_internal_message(message)
          case message["type"]
          when "disconnect"
            logger.info "Removing connection (#{connection_identifier})"
            close(reason: ActionCable::INTERNAL[:disconnect_reasons][:remote], reconnect: message.fetch("reconnect", true))
          end
        rescue Exception => e
          logger.error "There was an exception - #{e.class}(#{e.message})"
          logger.error e.backtrace.join("\n")

          close
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
actioncable-next-0.1.2 lib/action_cable/connection/internal_channel.rb
actioncable-next-0.1.1 lib/action_cable/connection/internal_channel.rb
actioncable-next-0.1.0 lib/action_cable/connection/internal_channel.rb