Skip to content

Commit

Permalink
Merge 861762e into 61d4708
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandred committed Sep 18, 2015
2 parents 61d4708 + 861762e commit e8add59
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/volt/tasks/query_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def add_listener(collection, query)
# For requests from the client (with @channel), we track the channel
# so we can send the results back. Server side requests don't stay live,
# they simply return to :dirty once the query is issued.
@channel.user_id = Volt.current_user_id
@channel.update_user_id(Volt.current_user_id)

# live_query.add_channel(@channel)
end
Expand Down
6 changes: 6 additions & 0 deletions app/volt/tasks/user_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ def login(login_info)
end
end
end

def logout(logout_info)
# Remove user_id from user's channel
@channel.update_user_id(nil) if @channel
end

end
26 changes: 26 additions & 0 deletions lib/volt/server/socket_connection_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ def initialize(session, *args)

@@channels ||= []
@@channels << self

end

def update_user_id(user_id)
if !@user_id && user_id
# If there is currently no user id associated with this channel
# and we get a new valid user_id, set it then trigger a
# user_connect event
@user_id = user_id
@@dispatcher.volt_app.trigger!("user_connect", @user_id)
elsif @user_id && !user_id
# If there is currently a user id associated with this channel
# and we get a nil user id, trigger a user_disconnect event then
# set the id to nil
@@dispatcher.volt_app.trigger!("user_disconnect", @user_id)
@user_id = user_id
else
# Otherwise, lets just set the id (should never really run)
@user_id = user_id
end
end

def self.dispatcher=(val)
Expand Down Expand Up @@ -85,6 +105,12 @@ def closed

begin
@@dispatcher.close_channel(self)

# Trigger a user disconnect event even if the user hasn't logged out
if @user_id
@@dispatcher.volt_app.trigger!("user_disconnect", @user_id)
end

rescue DRb::DRbConnError => e
# ignore drb read of @@dispatcher error if child has closed
end
Expand Down
3 changes: 3 additions & 0 deletions lib/volt/volt/server_setup/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
module Volt
module ServerSetup
module App
# Include Eventable to allow for lifecycle callbacks
include Eventable

# The root url is where the volt app is mounted
attr_reader :root_url
# The app url is where the app folder (and sprockets) is mounted
Expand Down
4 changes: 4 additions & 0 deletions lib/volt/volt/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def login(username, password)
end

def logout
# Notify the backend so we can remove the user_id from the user's channel
UserTasks.logout(user_id: Volt.current_app.cookies._user_id)

# Remove the cookie so user is no longer logged in
Volt.current_app.cookies.delete(:user_id)
end

Expand Down

0 comments on commit e8add59

Please sign in to comment.