spool.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. class Sessions::Event::Spool < Sessions::Event::Base
  2. =begin
  3. Event module to serve spool messages and send them to new client connection.
  4. To execute this manually, just paste the following into the browser console
  5. App.WebSocket.send({event:'spool'})
  6. =end
  7. def run
  8. # error handling
  9. if @payload['timestamp']
  10. log 'notice', "request spool data > '#{Time.at(@payload['timestamp']).utc.iso8601}'"
  11. else
  12. log 'notice', 'request spool with init data'
  13. end
  14. if !@session || !@session['id']
  15. log 'error', "Can't send spool, session not authenticated"
  16. return {
  17. event: 'error',
  18. data: {
  19. error: 'Can\'t send spool, session not authenticated',
  20. },
  21. }
  22. end
  23. spool = Sessions.spool_list(@payload['timestamp'], @session['id'])
  24. spool.each do |item|
  25. # create new msg to push to client
  26. if item[:type] == 'direct'
  27. log 'notice', "send spool to (user_id=#{@session['id']})"
  28. else
  29. log 'notice', 'send spool'
  30. end
  31. websocket_send(@client_id, item[:message])
  32. end
  33. # send spool:sent event to client
  34. log 'notice', 'send spool:sent event'
  35. {
  36. event: 'spool:sent',
  37. data: {
  38. timestamp: Time.now.utc.to_i,
  39. },
  40. }
  41. end
  42. end