Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix no implicit conversion of User into String (for sync enviroment) … #2

Open
wants to merge 3 commits into
base: threadsocket-rails
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/websocket_rails/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/websocket_rails/connection_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/websocket_rails/synchronization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/websocket_rails/user_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,16 @@ def remote_connection(identifier, user_hash)
# active connections belonging to the user.
class LocalConnection

delegate :sync, to: Synchronization

attr_reader :connections

def initialize
@connections = []
end

def <<(connection)
@connections << connection
@connections << connection unless @connections.include?(connection)
end

def delete(connection)
Expand Down