lib/anycable/rails/actioncable/connection.rb in anycable-rails-1.0.0.preview2 vs lib/anycable/rails/actioncable/connection.rb in anycable-rails-1.0.0.rc1

- old
+ new

@@ -1,10 +1,12 @@ # frozen_string_literal: true require "action_cable/connection" +require "anycable/rails/actioncable/connection/serializable_identification" require "anycable/rails/refinements/subscriptions" require "anycable/rails/actioncable/channel" +require "anycable/rails/actioncable/remote_connections" require "anycable/rails/session_proxy" module ActionCable module Connection # rubocop: disable Metrics/ClassLength @@ -13,30 +15,30 @@ # to re-use them in the subsequent calls LOG_TAGS_IDENTIFIER = "__ltags__" using AnyCable::Refinements::Subscriptions + include SerializableIdentification + attr_reader :socket delegate :env, :session, to: :request class << self def call(socket, **options) - new(socket, options) + new(socket, nil, options) end + end - def identified_by(*identifiers) - super - Array(identifiers).each do |identifier| - define_method(identifier) do - instance_variable_get(:"@#{identifier}") || fetch_identifier(identifier) - end - end + def initialize(socket, env, identifiers: "{}", subscriptions: []) + if env + # If env is set, then somehow we're in the context of Action Cable + # Return and print a warning in #process + @request = ActionDispatch::Request.new(env) + return end - end - def initialize(socket, identifiers: "{}", subscriptions: []) @ids = ActiveSupport::JSON.decode(identifiers) @ltags = socket.cstate.read(LOG_TAGS_IDENTIFIER).yield_self do |raw_tags| next unless raw_tags ActiveSupport::JSON.decode(raw_tags) @@ -49,10 +51,22 @@ # Initialize channels if any subscriptions.each { |id| @subscriptions.fetch(id) } end + def process + # Use Rails logger here to print to stdout in development + logger.error invalid_request_message + logger.info finished_request_message + [404, {"Content-Type" => "text/plain"}, ["Page not found"]] + end + + def invalid_request_message + "You're trying to connect to Action Cable server while using AnyCable. " \ + "See https://docs.anycable.io/v1/#/troubleshooting?id=server-raises-an-argumenterror-exception-when-client-tries-to-connect" + end + def handle_open logger.info started_request_message if access_logs? verify_origin! || return @@ -103,34 +117,9 @@ socket.close end def transmit(cable_message) socket.transmit encode(cable_message) - end - - # Generate identifiers info. - # Converts GlobalID compatible vars to corresponding global IDs params. - def identifiers_hash - identifiers.each_with_object({}) do |id, acc| - obj = instance_variable_get("@#{id}") - next unless obj - - acc[id] = obj.try(:to_gid_param) || obj - end.compact - end - - def identifiers_json - identifiers_hash.to_json - end - - # Fetch identifier and deserialize if neccessary - def fetch_identifier(name) - @cached_ids[name] ||= @cached_ids.fetch(name) do - val = ids[name.to_s] - next val unless val.is_a?(String) - - GlobalID::Locator.locate(val) || val - end end def logger @logger ||= TaggedLoggerProxy.new(AnyCable.logger, tags: ltags || []) end