From 8451f8653aa1b1f71a92deb0cf1f8092d8a56e2a Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 2 Oct 2012 01:03:08 -0400 Subject: [PATCH] user presence development --- lib/jam_websockets/client_context.rb | 12 +++--- lib/jam_websockets/router.rb | 55 ++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/lib/jam_websockets/client_context.rb b/lib/jam_websockets/client_context.rb index eb40fb215..706e578b6 100644 --- a/lib/jam_websockets/client_context.rb +++ b/lib/jam_websockets/client_context.rb @@ -5,14 +5,14 @@ module JamWebsockets def initialize(user, client) @user = user - @client = client - @msg_count = 0 - @session = nil + @client = client + @msg_count = 0 + @session = nil end - def to_s - return "Client[user:#{@user} client:#{@client} msgs:#{@msg_count} session:#{@session}]" - end + def to_s + return "Client[user:#{@user} client:#{@client} msgs:#{@msg_count} session:#{@session}]" + end end end diff --git a/lib/jam_websockets/router.rb b/lib/jam_websockets/router.rb index cc5edb92e..efade4a56 100644 --- a/lib/jam_websockets/router.rb +++ b/lib/jam_websockets/router.rb @@ -355,11 +355,15 @@ module JamWebsockets if !context.nil? - remove_user(context) + remove_user(context) - if !context.session.nil? - remove_session(context) - end + # remove this connection from the database + connection = Connection.delete_all "user_id = '#{context.user.id}' AND client_id = '#{context.client.client_id}'" + send_friend_update(user, false) + + if !context.session.nil? + remove_session(context) + end else @log.debug "skipping duplicate cleanup attempt of authorized client" end @@ -443,12 +447,6 @@ module JamWebsockets login_ack = @message_factory.login_ack(remote_ip) send_to_client(client, login_ack) - # log this connection in the database - connection = Connection.new() - connection.user_id = user.id - connection.client_id = client.client_id - connection.save - # remove from pending_queue @semaphore.synchronize do @pending_clients.delete(client) @@ -457,14 +455,49 @@ module JamWebsockets context = ClientContext.new(user, client) @clients[client] = context add_user(context) + + # log this connection in the database + connection = Connection.new(user.id, client.id) + + if connection.save? + send_friend_update(user, true) + end end else raise SessionError, 'invalid login' end end + def send_friend_update(user, online) + unless user.friends.nil? + @log.debug "sending friend update message to friends" + + # create the friend_update message + friend_update = @message_factory.friend_update(user.id, online) + + # send the friend_update to each friend that has active connections + user.friends.each do |friend| + # only send to friends that have active connections + active_connections = @user_context_lookup[friend.id] + unless active_connections.nil? + # send the update to each active connection of this friend + active_connections.each do |context| + EM.schedule do + @log.debug "sending friend update message to #{friend}" + send_to_client(context.client, friend_update) + end + end + end + end + end + end + def handle_heartbeat(heartbeat, client) - # todo: manage staleness + context = @clients[client] + @log.debug "updating timestamp for user #{context}" + connection = Connection.find_by_user_id_and_client_id(context.user.user_id, context.client.client_id) + connection.updated_at = DateTime.now + connection.save end def handle_join_jam_session(join_jam_session, client)