Merge pull request #355 from sharelatex/bg-handle-reconnects

wait for confirmation before sending joinProject
This commit is contained in:
Brian Gough 2016-11-01 11:50:44 +00:00 committed by GitHub
commit 69e957419d

View file

@ -52,8 +52,19 @@ define [], () ->
'connect timeout': 30 * 1000 'connect timeout': 30 * 1000
"force new connection": true "force new connection": true
# The "connect" event is the first event we get back. It only
# indicates that the websocket is connected, we still need to
# pass authentication to join a project.
@ide.socket.on "connect", () => @ide.socket.on "connect", () =>
sl_console.log "[socket.io connect] Connected" sl_console.log "[socket.io connect] Connected"
# The next event we should get is an authentication response
# from the server, either "connectionAccepted" or
# "connectionRejected".
@ide.socket.on 'connectionAccepted', (message) =>
sl_console.log "[socket.io connectionAccepted] allowed to connect"
@connected = true @connected = true
@gracefullyReconnecting = false @gracefullyReconnecting = false
@ide.pushEvent("connected") @ide.pushEvent("connected")
@ -64,16 +75,26 @@ define [], () ->
if @$scope.state.loading if @$scope.state.loading
@$scope.state.load_progress = 70 @$scope.state.load_progress = 70
# we have passed authentication so we can now join the project
setTimeout(() => setTimeout(() =>
@joinProject() @joinProject()
, 100) , 100)
@ide.socket.on 'connectionRejected', (err) =>
sl_console.log "[socket.io connectionRejected] session not valid or other connection error"
# we have failed authentication, usually due to an invalid session cookie
return @reportConnectionError(err)
# Alternatively the attempt to connect can fail completely, so
# we never get into the "connect" state.
@ide.socket.on "connect_failed", () => @ide.socket.on "connect_failed", () =>
@connected = false @connected = false
$scope.$apply () => $scope.$apply () =>
@$scope.state.error = "Unable to connect, please view the <u><a href='http://sharelatex.tenderapp.com/help/kb/latex-editor/editor-connection-problems'>connection problems guide</a></u> to fix the issue." @$scope.state.error = "Unable to connect, please view the <u><a href='http://sharelatex.tenderapp.com/help/kb/latex-editor/editor-connection-problems'>connection problems guide</a></u> to fix the issue."
# We can get a "disconnect" event at any point after the
# "connect" event.
@ide.socket.on 'disconnect', () => @ide.socket.on 'disconnect', () =>
sl_console.log "[socket.io disconnect] Disconnected" sl_console.log "[socket.io disconnect] Disconnected"
@ -86,6 +107,8 @@ define [], () ->
if !$scope.connection.forced_disconnect and !@userIsInactive and !@gracefullyReconnecting if !$scope.connection.forced_disconnect and !@userIsInactive and !@gracefullyReconnecting
@startAutoReconnectCountdown() @startAutoReconnectCountdown()
# Site administrators can send the forceDisconnect event to all users
@ide.socket.on 'forceDisconnect', (message) => @ide.socket.on 'forceDisconnect', (message) =>
@$scope.$apply () => @$scope.$apply () =>
@$scope.permissions.write = false @$scope.permissions.write = false
@ -104,20 +127,28 @@ define [], () ->
sl_console.log "Reconnect gracefully" sl_console.log "Reconnect gracefully"
@reconnectGracefully() @reconnectGracefully()
joinProject: () -> # Error reporting, which can reload the page if appropriate
sl_console.log "[joinProject] joining..."
@ide.socket.emit 'joinProject', { reportConnectionError: (err) ->
project_id: @ide.project_id sl_console.log "[socket.io] reporting connection error"
}, (err, project, permissionsLevel, protocolVersion) => if err?.message == "not authorized" or err?.message == "invalid session"
if err?
if err.message == "not authorized"
window.location = "/login?redir=#{encodeURI(window.location.pathname)}" window.location = "/login?redir=#{encodeURI(window.location.pathname)}"
else else
@ide.socket.disconnect() @ide.socket.disconnect()
@ide.showGenericMessageModal("Something went wrong connecting", """ @ide.showGenericMessageModal("Something went wrong connecting", """
Something went wrong connecting to your project. Please refresh is this continues to happen. Something went wrong connecting to your project. Please refresh is this continues to happen.
""") """)
return
joinProject: () ->
sl_console.log "[joinProject] joining..."
# Note: if the "joinProject" message doesn't reach the server
# (e.g. if we are in a disconnected state at this point) the
# callback will never be executed
@ide.socket.emit 'joinProject', {
project_id: @ide.project_id
}, (err, project, permissionsLevel, protocolVersion) =>
if err?
return @reportConnectionError(err)
if @$scope.protocolVersion? and @$scope.protocolVersion != protocolVersion if @$scope.protocolVersion? and @$scope.protocolVersion != protocolVersion
location.reload(true) location.reload(true)