Improved re-spool of spool messages after connection was down.

This commit is contained in:
Martin Edenhofer 2013-06-30 17:54:54 +02:00
parent 4dd231053e
commit f5c65bced4
3 changed files with 24 additions and 8 deletions

View file

@ -207,9 +207,7 @@ class _webSocketSingleton extends App.Controller
# take connection down and keep it down
return if @connectionKeepDown
# set timestamp to get spool messages later
if @connectionEstablished
@lastSpoolMessage = Math.round( +new Date()/1000 )
@connectionEstablished = false
return if @backend is 'ajax'
@ -248,6 +246,10 @@ class _webSocketSingleton extends App.Controller
# go through all blocks
for item in data
# set timestamp to get spool messages later
if item['spool']
@lastSpoolMessage = Math.round( +new Date()/1000 )
# reset reconnect loop
if item['action'] is 'pong'
@pong()

View file

@ -31,7 +31,13 @@ class LongPollingController < ApplicationController
# get spool messages and send them to new client connection
if params['data']['action'] == 'spool'
log 'notice', "request spool data", client_id
# error handling
if params['data']['timestamp']
log 'notice', "request spool data > '#{Time.at( params['data']['timestamp'] ).to_s}'", client_id
else
log 'notice', "request spool init data", client_id
end
if current_user
spool = Session.spool_list( params['data']['timestamp'], current_user.id )
@ -49,7 +55,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', :data => { :timestamp => Time.now.to_i } } )
Session.send( client_id, { :event => 'spool:sent', :data => { :timestamp => Time.now.utc.to_i } } )
end

View file

@ -55,7 +55,7 @@ OptionParser.new do |opts|
end.parse!
if ARGV[0] != 'start' && ARGV[0] != 'stop'
puts "Usage: websocket-server.rb start|stop [options]"
puts "Usage: #{File.basename(__FILE__)} start|stop [options]"
exit;
end
@ -137,9 +137,15 @@ EventMachine.run {
# get spool messages and send them to new client connection
if data['action'] == 'spool'
log 'notice', "request spool data", client_id
if @clients[client_id][:session]['id']
# error handling
if data['timestamp']
log 'notice', "request spool data > '#{Time.at(data['timestamp']).to_s}'", client_id
else
log 'notice', "request spool with init data", client_id
end
if @clients[client_id] && @clients[client_id][:session] && @clients[client_id][:session]['id']
spool = Session.spool_list( data['timestamp'], @clients[client_id][:session]['id'] )
spool.each { |item|
@ -153,11 +159,13 @@ EventMachine.run {
@clients[client_id][:websocket].send( "[#{ msg }]" )
end
}
else
log 'error', "can't send spool, session not authenticated", client_id
end
# send spool:sent event to client
log 'notice', "send spool:sent event", client_id
@clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.now.to_i.to_s + '}}]' )
@clients[client_id][:websocket].send( '[{"event":"spool:sent","data":{"timestamp":' + Time.now.utc.to_i.to_s + '}}]' )
end
# get session