Reconnect gracefully when asked to by real-time

This commit is contained in:
James Allen 2016-10-24 16:36:40 +01:00
parent 49fecc9ef4
commit 52754e038a
2 changed files with 33 additions and 3 deletions

View file

@ -27,6 +27,7 @@ define [], () ->
@connected = false @connected = false
@userIsInactive = false @userIsInactive = false
@gracefullyReconnecting = false
@$scope.connection = @$scope.connection =
reconnecting: false reconnecting: false
@ -54,6 +55,7 @@ define [], () ->
@ide.socket.on "connect", () => @ide.socket.on "connect", () =>
sl_console.log "[socket.io connect] Connected" sl_console.log "[socket.io connect] Connected"
@connected = true @connected = true
@gracefullyReconnecting = false
@ide.pushEvent("connected") @ide.pushEvent("connected")
@$scope.$apply () => @$scope.$apply () =>
@ -81,7 +83,7 @@ define [], () ->
@$scope.$apply () => @$scope.$apply () =>
@$scope.connection.reconnecting = false @$scope.connection.reconnecting = false
if !$scope.connection.forced_disconnect and !@userIsInactive if !$scope.connection.forced_disconnect and !@userIsInactive and !@gracefullyReconnecting
@startAutoReconnectCountdown() @startAutoReconnectCountdown()
@ide.socket.on 'forceDisconnect', (message) => @ide.socket.on 'forceDisconnect', (message) =>
@ -97,7 +99,11 @@ define [], () ->
setTimeout () -> setTimeout () ->
location.reload() location.reload()
, 10 * 1000 , 10 * 1000
@ide.socket.on "reconnectGracefully", () =>
sl_console.log "Reconnect gracefully"
@reconnectGracefully()
joinProject: () -> joinProject: () ->
sl_console.log "[joinProject] joining..." sl_console.log "[joinProject] joining..."
@ide.socket.emit 'joinProject', { @ide.socket.emit 'joinProject', {
@ -180,3 +186,22 @@ define [], () ->
@$scope.$apply () => @$scope.$apply () =>
@$scope.connection.inactive_disconnect = true @$scope.connection.inactive_disconnect = true
RECONNECT_GRACEFULLY_RETRY_INTERVAL: 5000 # ms
MAX_RECONNECT_GRACEFULLY_INTERVAL: 60 * 5 * 1000 # 5 minutes
reconnectGracefully: () ->
@reconnectGracefullyStarted ?= new Date()
userIsInactive = (new Date() - @lastUserAction) > @RECONNECT_GRACEFULLY_RETRY_INTERVAL
maxIntervalReached = (new Date() - @reconnectGracefullyStarted) > @MAX_RECONNECT_GRACEFULLY_INTERVAL
if userIsInactive or maxIntervalReached
sl_console.log "[reconnectGracefully] User didn't do anything for last 5 seconds, reconnecting"
@_reconnectGracefullyNow()
else
sl_console.log "[reconnectGracefully] User is working, will try again in 5 seconds"
setTimeout () =>
@reconnectGracefully()
, @RECONNECT_GRACEFULLY_RETRY_INTERVAL
_reconnectGracefullyNow: () ->
@gracefullyReconnecting = true
@reconnectGracefullyStarted = null
@reconnectImmediately()

View file

@ -17,8 +17,13 @@ define [
# When we join the project: # When we join the project:
# index all references files # index all references files
# and don't broadcast to all clients # and don't broadcast to all clients
@inited = false
@$scope.$on 'project:joined', (e) => @$scope.$on 'project:joined', (e) =>
@indexAllReferences(false) # We only need to grab the references when the editor first loads,
# not on every reconnect
if !@inited
@inited = true
@indexAllReferences(false)
setTimeout( setTimeout(
(self) -> (self) ->