2013-09-21 22:40:19 +00:00
|
|
|
class Sessions::Client
|
2013-09-29 23:46:11 +00:00
|
|
|
|
2013-09-21 22:40:19 +00:00
|
|
|
def initialize( client_id )
|
|
|
|
@client_id = client_id
|
2015-05-04 19:45:48 +00:00
|
|
|
self.log '---client start ws connection---'
|
2013-09-21 22:40:19 +00:00
|
|
|
self.fetch
|
2015-05-04 19:45:48 +00:00
|
|
|
self.log '---client exiting ws connection---'
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
backends = [
|
|
|
|
'Sessions::Backend::TicketOverviewIndex',
|
|
|
|
'Sessions::Backend::TicketOverviewList',
|
|
|
|
'Sessions::Backend::Collections',
|
|
|
|
'Sessions::Backend::Rss',
|
|
|
|
'Sessions::Backend::ActivityStream',
|
|
|
|
'Sessions::Backend::TicketCreate',
|
|
|
|
]
|
|
|
|
|
|
|
|
backend_pool = []
|
|
|
|
user_id_last_run = nil
|
2013-09-21 22:40:19 +00:00
|
|
|
loop_count = 0
|
2015-05-05 14:10:06 +00:00
|
|
|
loop do
|
2013-09-21 22:40:19 +00:00
|
|
|
|
|
|
|
# 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']
|
2015-04-27 13:42:53 +00:00
|
|
|
user = User.lookup( id: session_data[:user]['id'] )
|
2013-09-21 22:40:19 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
# release old objects
|
2015-05-07 09:49:46 +00:00
|
|
|
backend_pool.collect! {
|
|
|
|
nil
|
2014-07-13 23:29:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# create new pool
|
2014-07-13 18:52:32 +00:00
|
|
|
backend_pool = []
|
|
|
|
backends.each {|backend|
|
|
|
|
item = backend.constantize.new(user, self, @client_id)
|
|
|
|
backend_pool.push item
|
|
|
|
}
|
|
|
|
end
|
2013-09-21 22:40:19 +00:00
|
|
|
|
|
|
|
loop_count += 1
|
2015-05-04 19:45:48 +00:00
|
|
|
self.log "---client - looking for data of user #{user.id}"
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# push messages from backends
|
2015-05-07 10:25:16 +00:00
|
|
|
backend_pool.each(&:push)
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2015-05-04 19:45:48 +00:00
|
|
|
self.log '---/client-'
|
2013-09-21 22:40:19 +00:00
|
|
|
|
|
|
|
# start faster in the beginnig
|
|
|
|
if loop_count < 20
|
|
|
|
sleep 0.6
|
|
|
|
else
|
|
|
|
sleep 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# send update to browser
|
|
|
|
def send( data )
|
|
|
|
Sessions.send( @client_id, data )
|
|
|
|
end
|
|
|
|
|
2015-05-04 19:45:48 +00:00
|
|
|
def log( msg )
|
2015-05-04 20:02:00 +00:00
|
|
|
Rails.logger.debug "client(#{ @client_id }) #{ msg }"
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|