client.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. class Sessions::Client
  2. def initialize(client_id)
  3. @client_id = client_id
  4. log '---client start ws connection---'
  5. fetch
  6. log '---client exiting ws connection---'
  7. end
  8. def fetch
  9. backends = [
  10. 'Sessions::Backend::TicketOverviewList',
  11. 'Sessions::Backend::Collections',
  12. 'Sessions::Backend::Rss',
  13. 'Sessions::Backend::ActivityStream',
  14. 'Sessions::Backend::TicketCreate',
  15. ]
  16. asset_lookup = {}
  17. backend_pool = []
  18. user_id_last_run = nil
  19. loop_count = 0
  20. loop do
  21. # get connection user
  22. session_data = Sessions.get(@client_id)
  23. return if !session_data
  24. return if !session_data[:user]
  25. return if !session_data[:user]['id']
  26. user = User.lookup(id: session_data[:user]['id'])
  27. return if !user
  28. # init new backends
  29. if user_id_last_run != user.id
  30. user_id_last_run = user.id
  31. asset_lookup = {}
  32. # release old objects
  33. backend_pool.collect! {
  34. nil
  35. }
  36. # create new pool
  37. backend_pool = []
  38. backends.each {|backend|
  39. item = backend.constantize.new(user, asset_lookup, self, @client_id)
  40. backend_pool.push item
  41. }
  42. end
  43. loop_count += 1
  44. log "---client - looking for data of user #{user.id}"
  45. # push messages from backends
  46. backend_pool.each(&:push)
  47. log '---/client-'
  48. # start faster in the beginnig
  49. if loop_count < 20
  50. sleep 0.6
  51. else
  52. sleep 1
  53. end
  54. end
  55. end
  56. # send update to browser
  57. def send(data)
  58. Sessions.send(@client_id, data)
  59. end
  60. def log(msg)
  61. Rails.logger.debug "client(#{@client_id}) #{msg}"
  62. end
  63. end