1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actioncable/lib/action_cable/connection/internal_channel.rb

46 lines
1.4 KiB
Ruby
Raw Normal View History

module ActionCable
module Connection
2015-07-07 16:42:02 -04:00
# 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(message) }
@_internal_subscriptions ||= []
@_internal_subscriptions << [ internal_channel, callback ]
server.event_loop.post { 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| server.event_loop.post { pubsub.unsubscribe(channel, callback) } }
end
end
def process_internal_message(message)
message = ActiveSupport::JSON.decode(message)
case message['type']
when 'disconnect'
logger.info "Removing connection (#{connection_identifier})"
2015-06-27 10:57:32 -04:00
websocket.close
end
rescue Exception => e
logger.error "There was an exception - #{e.class}(#{e.message})"
logger.error e.backtrace.join("\n")
2015-06-21 15:45:37 -04:00
close
end
end
end
end