client.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. class Sessions::Client
  2. def initialize( client_id )
  3. @client_id = client_id
  4. self.log 'notify', "---client start ws connection---"
  5. self.fetch
  6. self.log 'notify', "---client exiting ws connection---"
  7. end
  8. def fetch
  9. backends = [
  10. 'Sessions::Backend::TicketOverviewIndex',
  11. 'Sessions::Backend::TicketOverviewList',
  12. 'Sessions::Backend::Collections',
  13. 'Sessions::Backend::Rss',
  14. 'Sessions::Backend::ActivityStream',
  15. 'Sessions::Backend::TicketCreate',
  16. ]
  17. backend_pool = []
  18. user_id_last_run = nil
  19. loop_count = 0
  20. while true
  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. # release old objects
  32. backend_pool.each {|pool|
  33. pool = nil
  34. }
  35. # create new pool
  36. backend_pool = []
  37. backends.each {|backend|
  38. item = backend.constantize.new(user, self, @client_id)
  39. backend_pool.push item
  40. }
  41. end
  42. loop_count += 1
  43. self.log 'notice', "---client - looking for data of user #{user.id}"
  44. # push messages from backends
  45. backend_pool.each {|pool|
  46. pool.push
  47. }
  48. self.log 'notice', "---/client-"
  49. # start faster in the beginnig
  50. if loop_count < 20
  51. sleep 0.6
  52. else
  53. sleep 1
  54. end
  55. end
  56. end
  57. # send update to browser
  58. def send( data )
  59. Sessions.send( @client_id, data )
  60. end
  61. def log( level, data )
  62. return if level == 'notice'
  63. puts "#{Time.now}:client(#{ @client_id }) #{ data }"
  64. end
  65. end