user presence development
This commit is contained in:
parent
af4a94c875
commit
8451f8653a
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue