class WebsocketRails::UserManager::LocalConnection

The UserManager::LocalConnection Class serves as a proxy object for storing multiple connections that belong to the same user. It implements the same basic interface as a Connection. This allows you to work with the object as though it is a single connection, but still trigger the events on all active connections belonging to the user.

Attributes

connections[R]

Public Class Methods

new() click to toggle source
# File lib/websocket_rails/user_manager.rb, line 136
def initialize
  @connections = []
end

Public Instance Methods

<<(connection) click to toggle source
# File lib/websocket_rails/user_manager.rb, line 140
def <<(connection)
  @connections << connection
end
connected?() click to toggle source
# File lib/websocket_rails/user_manager.rb, line 148
def connected?
  true
end
delete(connection) click to toggle source
# File lib/websocket_rails/user_manager.rb, line 144
def delete(connection)
  @connections.delete(connection)
end
send_message(event_name, data = {}, options = {}) click to toggle source
# File lib/websocket_rails/user_manager.rb, line 166
def send_message(event_name, data = {}, options = {})
  options.merge! :user_id => user_identifier
  options[:data] = data

  event = Event.new(event_name, options)

  # Trigger the event on all active connections for this user.
  connections.each do |connection|
    connection.trigger event
  end

  # Still publish the event in case the user is connected to
  # other workers as well.
  Synchronization.publish event if WebsocketRails.synchronize?
  true
end
trigger(event) click to toggle source
# File lib/websocket_rails/user_manager.rb, line 160
def trigger(event)
  connections.each do |connection|
    connection.trigger event
  end
end
user() click to toggle source
# File lib/websocket_rails/user_manager.rb, line 156
def user
  latest_connection.user
end
user_identifier() click to toggle source
# File lib/websocket_rails/user_manager.rb, line 152
def user_identifier
  latest_connection.user_identifier
end

Private Instance Methods

latest_connection() click to toggle source
# File lib/websocket_rails/user_manager.rb, line 185
def latest_connection
  @connections.last
end