trabajo-afectivo/lib/sessions/client.rb

81 lines
1.8 KiB
Ruby
Raw Normal View History

class Sessions::Client
2013-09-29 23:46:11 +00:00
def initialize(client_id, node_id)
@client_id = client_id
@node_id = node_id
log '---client start ws connection---'
fetch
log '---client exiting ws connection---'
end
def fetch
2014-07-13 18:52:32 +00:00
backends = [
'Sessions::Backend::TicketOverviewList',
'Sessions::Backend::Collections',
'Sessions::Backend::ActivityStream',
'Sessions::Backend::TicketCreate',
]
2016-03-03 08:56:13 +00:00
asset_lookup = {}
2014-07-13 18:52:32 +00:00
backend_pool = []
user_id_last_run = nil
loop_count = 0
loop do
# check if session still exists
return if !Sessions.session_exists?(@client_id)
# get connection user
session_data = Sessions.get(@client_id)
return if !session_data
return if !session_data[:user]
2014-10-13 23:58:21 +00:00
return if !session_data[:user]['id']
user = User.lookup(id: session_data[:user]['id'])
return if !user
2014-07-13 18:52:32 +00:00
# init new backends
if user_id_last_run != user.id
2014-07-13 23:29:29 +00:00
user_id_last_run = user.id
2016-03-03 08:56:13 +00:00
asset_lookup = {}
2014-07-13 23:29:29 +00:00
# release old objects
backend_pool.collect! do
nil
end
2014-07-13 23:29:29 +00:00
# create new pool
2014-07-13 18:52:32 +00:00
backend_pool = []
backends.each do |backend|
2016-03-03 08:56:13 +00:00
item = backend.constantize.new(user, asset_lookup, self, @client_id)
2014-07-13 18:52:32 +00:00
backend_pool.push item
end
2014-07-13 18:52:32 +00:00
end
loop_count += 1
log "---client - looking for data of user #{user.id}"
2014-07-13 18:52:32 +00:00
# push messages from backends
backend_pool.each(&:push)
log '---/client-'
# start faster in the beginnig
if loop_count < 20
sleep 1
2018-01-24 12:55:15 +00:00
else
sleep 2.2
end
end
end
# send update to browser
def send(data)
Sessions.send(@client_id, data)
end
def log(msg)
Rails.logger.debug "client(#{@node_id}.#{@client_id}) #{msg}"
end
end