diff --git a/lib/websocket_rails/connection.rb b/lib/websocket_rails/connection.rb index 9548a67d..3e321af6 100644 --- a/lib/websocket_rails/connection.rb +++ b/lib/websocket_rails/connection.rb @@ -10,7 +10,7 @@ def self.websocket?(env) end attr_reader :id, :dispatcher, :queue, :env, :request, :data_store, - :websocket, :message_handler + :websocket, :message_handler, :current_device delegate :supported_protocols, to: WebsocketRails delegate :on_open, :on_message, :on_close, :on_error, to: :message_handler @@ -26,6 +26,7 @@ def initialize(request, dispatcher) @delegate = WebsocketRails::DelegationController.new @delegate.instance_variable_set(:@_env, request.env) @delegate.instance_variable_set(:@_request, request) + @current_device = nil bind_message_handler rescue => ex @@ -108,6 +109,15 @@ def user_identifier end end + def register_user(device = nil) + @current_device = device + dispatcher.connection_manager.register_user_connection self + end + + def destroy_user + dispatcher.connection_manager.destroy_user_connection self + end + private def dispatch(event) diff --git a/lib/websocket_rails/connection_manager.rb b/lib/websocket_rails/connection_manager.rb index 1de07d22..2e926ce1 100644 --- a/lib/websocket_rails/connection_manager.rb +++ b/lib/websocket_rails/connection_manager.rb @@ -103,11 +103,13 @@ def register_user_connection(connection) return unless connection.user_connection? WebsocketRails.users[connection.user_identifier] = connection end + public :register_user_connection def destroy_user_connection(connection) return unless connection.user_connection? WebsocketRails.users.delete(connection) end + public :destroy_user_connection end end diff --git a/lib/websocket_rails/synchronization.rb b/lib/websocket_rails/synchronization.rb index 2466d851..841b8e27 100644 --- a/lib/websocket_rails/synchronization.rb +++ b/lib/websocket_rails/synchronization.rb @@ -132,7 +132,7 @@ def remove_server(token) def register_remote_user(connection) id = connection.user_identifier user = connection.user - @active_users[id] = user + @active_users[id] = user.is_a?(String) ? user : user.to_json end def destroy_remote_user(identifier) diff --git a/lib/websocket_rails/user_manager.rb b/lib/websocket_rails/user_manager.rb index 970668f4..25179a66 100644 --- a/lib/websocket_rails/user_manager.rb +++ b/lib/websocket_rails/user_manager.rb @@ -133,6 +133,8 @@ def remote_connection(identifier, user_hash) # active connections belonging to the user. class LocalConnection + delegate :sync, to: Synchronization + attr_reader :connections def initialize @@ -140,7 +142,7 @@ def initialize end def <<(connection) - @connections << connection + @connections << connection unless @connections.include?(connection) end def delete(connection)