From 7a7a956f24fd389393350c56b9e1dbbfe4df39e4 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 17 Jun 2013 10:44:28 +0200 Subject: [PATCH] Improved spool feature to use server site timestamps (to have no conflict with different times on server/client). --- .../app/lib/app_post/websocket.js.coffee | 28 ++++++++++++++++--- app/controllers/long_polling_controller.rb | 2 +- script/websocket-server.rb | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee index 64e1250d9..52e074e9e 100644 --- a/app/assets/javascripts/app/lib/app_post/websocket.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/websocket.js.coffee @@ -37,6 +37,7 @@ class _Singleton extends App.Controller queue: [] supported: true lastSpoolMessage: undefined + sentSpoolFinished: true connectionKeepDown: false connectionEstablished: false connectionWasEstablished: false @@ -48,7 +49,7 @@ class _Singleton extends App.Controller super # on auth, send new auth data to server - App.Event.bind 'auth', => + App.Event.bind 'auth', (data) => @auth() # bind to send messages @@ -60,6 +61,21 @@ class _Singleton extends App.Controller @spool() ) + # get spool:sent + App.Event.bind( 'spool:sent', (data) => + + # set timestamp to get spool messages later + if !data + @lastSpoolMessage = Math.round( +new Date()/1000 ) + else + @lastSpoolMessage = data.timestamp + + # set sentSpoolFinished + @sentSpoolFinished = true + + @clearDelay 'reset-spool-sent-if-not-returned', 'ws' + ) + # inital connect @connect() @@ -93,6 +109,8 @@ class _Singleton extends App.Controller @send(data) spool: => + return if !@sentSpoolFinished + @sentSpoolFinished = false # build data to send to server data = @@ -102,15 +120,17 @@ class _Singleton extends App.Controller @log 'Websocket', 'debug', 'spool', data + # reset @sentSpoolFinished if spool:sent will not return + reset = => + @sentSpoolFinished = true + @delay reset, 60000, 'reset-spool-sent-finished-if-not-returned', 'ws' + # ask for spool messages App.Event.trigger( 'ws:send' data ) - # set timestamp to get spool messages later - @lastSpoolMessage = Math.round( +new Date()/1000 ) - close: ( params = {} ) => if params['force'] @connectionKeepDown = true diff --git a/app/controllers/long_polling_controller.rb b/app/controllers/long_polling_controller.rb index 727e8006e..6617effbd 100644 --- a/app/controllers/long_polling_controller.rb +++ b/app/controllers/long_polling_controller.rb @@ -47,7 +47,7 @@ class LongPollingController < ApplicationController # send spool:sent event to client sleep 0.2 log 'notice', "send spool:sent event", client_id - Session.send( client_id, { :event => 'spool:sent' } ) + Session.send( client_id, { :event => 'spool:sent', :timestamp => Time.now.to_i } ) end diff --git a/script/websocket-server.rb b/script/websocket-server.rb index 55a3a8862..ec51b2f13 100755 --- a/script/websocket-server.rb +++ b/script/websocket-server.rb @@ -154,7 +154,7 @@ EventMachine.run { # send spool:sent event to client log 'notice', "send spool:sent event", client_id - @clients[client_id][:websocket].send( '[{"event":"spool:sent"}]' ) + @clients[client_id][:websocket].send( '[{"event":"spool:sent","time":' + Time.now.to_i.to_s + '}]' ) end # get session