added method stale_client, cleanup_clients_with_ids; in handle_heartbeat added updating of connection state

This commit is contained in:
Jonathan Kolyer 2013-02-21 11:49:32 -06:00
parent 245ab0583b
commit 71cea79f36
1 changed files with 41 additions and 9 deletions

View File

@ -198,7 +198,6 @@ module JamWebsockets
def new_client(client)
@semaphore.synchronize do
@pending_clients.add(client)
end
@ -221,8 +220,7 @@ module JamWebsockets
client.onclose {
@log.debug "Connection closed"
cleanup_client(client)
stale_client(client)
}
client.onerror { |error|
@ -321,10 +319,38 @@ module JamWebsockets
cleanup
end
# caused a client connection to be marked stale
def stale_client(client)
if cid = client.client_id
ConnectionManager.active_record_transaction do |connection_manager|
connection_manager.flag_connection_stale_with_client_id(cid)
end
end
end
def cleanup_clients_with_ids(client_ids)
@log.debug("*** cleanup_clients_with_ids: client_ids = #{client_ids.inspect}")
client_ids.each do |cid|
if 0 < (ws_clients = @clients.keys).length
ws_clients.each do |client|
if cid == client.client_id
self.cleanup_client(client)
break
else
@log.debug("*** cleanup_clients: deleting connection = #{cid}")
ConnectionManager.active_record_transaction { |mgr| mgr.delete_connection(cid) }
end
end
else
ConnectionManager.active_record_transaction { |mgr| mgr.delete_connection(cid) }
end
end
end
# removes all resources associated with a client
def cleanup_client(client)
@semaphore.synchronize do
# @log.debug("*** cleanup_clients: client = #{client}")
pending = @pending_clients.delete?(client)
if !pending.nil?
@ -395,6 +421,7 @@ module JamWebsockets
end
def handle_server_directed(client_msg, client)
# @log.info("*** handle_server_directed(#{client_msg}, #{client})")
if client_msg.type == ClientMessage::Type::LOGIN
@ -411,6 +438,8 @@ module JamWebsockets
def handle_login(login, client)
@log.info("*** handle_login: login=#{login}; client=#{client}")
username = login.username if login.value_for_tag(1)
password = login.password if login.value_for_tag(2)
token = login.token if login.value_for_tag(3)
@ -429,7 +458,7 @@ module JamWebsockets
if !user.nil?
@log.debug "user #{user.email} logged in"
@log.debug "user #{user} logged in"
# respond with LOGIN_ACK to let client know it was successful
#binding.pry
@ -495,8 +524,11 @@ module JamWebsockets
@log.debug "unable to find connection due to heartbeat from client: #{context}"
else
@log.debug "updating connection freshness due to heartbeat from client: #{context}"
connection.updated_at = DateTime.now
connection.save(:validate => false) # validates are unneeded, as we are just touching updated_at
if connection.stale?
connection.connect!
else
connection.touch
end
end
end
@ -505,10 +537,10 @@ module JamWebsockets
if !token.nil? && token != ''
@log.debug "logging in via token"
# attempt login with token
user = User.find_by_remember_token(token)
user = JamRuby::User.find_by_remember_token(token)
if user.nil?
@log.debug "no user found with token"
@log.debug "no user found with token #{token}"
return false
else
@log.debug "#{user} login via token"