Merge pull request #349 from sharelatex/ja-real-time-reconnects

Reconnect gracefully when asked to by real-time
This commit is contained in:
James Allen 2016-10-26 10:04:25 +01:00 committed by GitHub
commit 032165c930
2 changed files with 35 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) =>
@ -98,6 +100,10 @@ define [], () ->
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,24 @@ 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
# Clear cookie so we don't go to the same backend server
$.cookie("SERVERID", "", { expires: -1, path: "/" })
@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) ->