minor refactor to be consistent with session directed messages

This commit is contained in:
Brian Smith 2012-10-07 16:51:43 -04:00
parent c3616a0cb3
commit 76883a0d7e
1 changed files with 18 additions and 13 deletions

View File

@ -372,8 +372,11 @@ module JamWebsockets
remove_user(context)
# remove this connection from the database
#JamRuby::Connection.delete_all "user_id = '#{context.user.id}' AND client_id = '#{context.client.client_id}'"
#send_friend_update(context.user, false)
if !context.user.nil? && !context.client.nil?
JamRuby::Connection.delete_all "user_id = '#{context.user.id}' AND client_id = '#{context.client.client_id}'"
end
send_friend_update(context.user, false, context.client)
if !context.session.nil?
remove_session(context)
@ -406,13 +409,13 @@ module JamWebsockets
elsif @message_factory.session_directed? client_msg
session = client_msg.target[MessageFactory::SESSION_TARGET_PREFIX.length..-1]
handle_session_directed(session, client_msg, client)
session_id = client_msg.target[MessageFactory::SESSION_TARGET_PREFIX.length..-1]
handle_session_directed(session_id, client_msg, client)
elsif @message_factory.user_directed? client_msg
user = client_msg.target[MessageFactory::USER_PREFIX_TARGET.length..-1]
handle_user_directed(user, client_msg, client)
user_id = client_msg.target[MessageFactory::USER_PREFIX_TARGET.length..-1]
handle_user_directed(user_id, client_msg, client)
else
raise SessionError, "client_msg.target is unknown type: #{client_msg.target}"
@ -475,7 +478,7 @@ module JamWebsockets
@log.debug "Created connection => #{connection.user}, #{connection.client_id}"
if connection.save
send_friend_update(user, true)
send_friend_update(user, true, context.client)
end
end
else
@ -483,10 +486,10 @@ module JamWebsockets
end
end
def send_friend_update(user, online)
def send_friend_update(user, online, client)
@log.debug "sending friend update for user #{user} online = #{online}"
if user.friends.exists?
if !user.nil? && user.friends.exists?
@log.debug "user has friends - sending friend updates"
# create the friend_update message
@ -496,8 +499,7 @@ module JamWebsockets
user.friends.each do |friend|
@log.debug "sending friend update message to #{friend}"
# put it on the topic exchange for users
@users_exchange.publish(friend_update_msg.to_s, :routing_key => "user.#{friend.id}")
handle_user_directed(friend.id, friend_update_msg, client)
end
end
end
@ -617,9 +619,12 @@ module JamWebsockets
@sessions_exchange.publish(client_msg.to_s, :routing_key => "session.#{session_id}", :properties => { :headers => { "client_id" => client.client_id } } )
end
def handle_user_directed(user, client_msg, client)
def handle_user_directed(user_id, client_msg, client)
raise SessionError, 'not implemented'
@log.debug "publishing to user #{user_id} from client_id #{client.client_id}"
# put it on the topic exchange for users
@users_exchange.publish(client_msg.to_s, :routing_key => "user.#{user_id}")
end
end
end