class WebsocketRails::UserManager
Attributes
Public Class Methods
# File lib/websocket_rails/user_manager.rb, line 40 def initialize @users = {} end
Public Instance Methods
# File lib/websocket_rails/user_manager.rb, line 44 def [](identifier) unless user = (@users[identifier.to_s] || find_remote_user(identifier.to_s)) user = MissingConnection.new(identifier.to_s) end user end
# File lib/websocket_rails/user_manager.rb, line 51 def []=(identifier, connection) @users[identifier.to_s] ||= LocalConnection.new @users[identifier.to_s] << connection Synchronization.register_user(connection) if WebsocketRails.synchronize? end
# File lib/websocket_rails/user_manager.rb, line 57 def delete(connection) identifier = connection.user_identifier.to_s if (@users.has_key?(identifier) && @users[identifier].connections.count > 1) @users[identifier].delete(connection) else @users.delete(identifier) Synchronization.destroy_user(identifier) if WebsocketRails.synchronize? end end
Behaves similarly to Ruby's Array#each, yielding each connection object stored in the {UserManager}. If synchronization is enabled, each connection from every active worker will be yielded.
You can access the `current_user` object through the user method.
You can trigger an event on this user using the send_message method which behaves identically to BaseController#send_message
.
If Synchronization
is enabled, the state of the `current_user` object will be equivalent to it's state at the time the connection was opened. It will not reflect changes made after the connection has been opened.
# File lib/websocket_rails/user_manager.rb, line 80 def each(&block) if WebsocketRails.synchronize? users_hash = Synchronization.all_users || return users_hash.each do |identifier, user_json| connection = remote_connection_from_json(identifier, user_json) block.call(connection) if block end else users.each do |_, connection| block.call(connection) if block end end end
Behaves similarly to Ruby's Array#map, invoking the given block with each active connection object and returning a new array with the results.
See UserManager#each
for details on the current usage and limitations.
# File lib/websocket_rails/user_manager.rb, line 98 def map(&block) collection = [] each do |connection| collection << block.call(connection) if block end collection end
Private Instance Methods
# File lib/websocket_rails/user_manager.rb, line 110 def find_remote_user(identifier) return unless WebsocketRails.synchronize? user_hash = Synchronization.find_user(identifier) || return remote_connection identifier, user_hash end
# File lib/websocket_rails/user_manager.rb, line 122 def remote_connection(identifier, user_hash) RemoteConnection.new identifier, user_hash end
# File lib/websocket_rails/user_manager.rb, line 117 def remote_connection_from_json(identifier, user_json) user_hash = JSON.parse(user_json) remote_connection identifier, user_hash end