Improved spool feature to use server site timestamps (to have no conflict with different times on server/client).

This commit is contained in:
Martin Edenhofer 2013-06-17 10:44:28 +02:00
parent 12acacfc47
commit 7a7a956f24
3 changed files with 26 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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