2015-04-06 13:21:22 -04:00
|
|
|
module ActionCable
|
|
|
|
module Connection
|
2015-07-07 16:42:02 -04:00
|
|
|
# Makes it possible for the RemoteConnection to disconnect a specific connection.
|
2015-04-09 18:26:18 -04:00
|
|
|
module InternalChannel
|
2015-04-06 13:21:22 -04:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2015-07-07 16:36:18 -04:00
|
|
|
private
|
2016-01-06 17:16:02 -05:00
|
|
|
def internal_channel
|
2015-07-07 16:36:18 -04:00
|
|
|
"action_cable/#{connection_identifier}"
|
|
|
|
end
|
2015-06-21 15:08:20 -04:00
|
|
|
|
2015-07-07 16:36:18 -04:00
|
|
|
def subscribe_to_internal_channel
|
|
|
|
if connection_identifier.present?
|
|
|
|
callback = -> (message) { process_internal_message(message) }
|
2016-01-06 17:16:02 -05:00
|
|
|
@_internal_subscriptions ||= []
|
|
|
|
@_internal_subscriptions << [ internal_channel, callback ]
|
2015-04-06 13:21:22 -04:00
|
|
|
|
2016-03-01 19:50:19 -05:00
|
|
|
server.event_loop.post { pubsub.subscribe(internal_channel, callback) }
|
2015-07-07 16:36:18 -04:00
|
|
|
logger.info "Registered connection (#{connection_identifier})"
|
|
|
|
end
|
2015-04-06 13:21:22 -04:00
|
|
|
end
|
|
|
|
|
2015-07-07 16:36:18 -04:00
|
|
|
def unsubscribe_from_internal_channel
|
2016-01-06 17:16:02 -05:00
|
|
|
if @_internal_subscriptions.present?
|
2016-03-01 19:50:19 -05:00
|
|
|
@_internal_subscriptions.each { |channel, callback| server.event_loop.post { pubsub.unsubscribe(channel, callback) } }
|
2015-07-07 16:36:18 -04:00
|
|
|
end
|
2015-04-06 13:21:22 -04:00
|
|
|
end
|
|
|
|
|
2015-04-09 18:26:18 -04:00
|
|
|
def process_internal_message(message)
|
2015-04-06 13:21:22 -04:00
|
|
|
message = ActiveSupport::JSON.decode(message)
|
|
|
|
|
|
|
|
case message['type']
|
|
|
|
when 'disconnect'
|
2015-04-10 15:23:44 -04:00
|
|
|
logger.info "Removing connection (#{connection_identifier})"
|
2015-06-27 10:57:32 -04:00
|
|
|
websocket.close
|
2015-04-06 13:21:22 -04:00
|
|
|
end
|
|
|
|
rescue Exception => e
|
2015-04-10 15:23:44 -04:00
|
|
|
logger.error "There was an exception - #{e.class}(#{e.message})"
|
|
|
|
logger.error e.backtrace.join("\n")
|
2015-04-06 13:21:22 -04:00
|
|
|
|
2015-06-21 15:45:37 -04:00
|
|
|
close
|
2015-04-06 13:21:22 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|