ConnectionManager.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. define [], () ->
  2. ONEHOUR = 1000 * 60 * 60
  3. class ConnectionManager
  4. disconnectAfterMs: ONEHOUR * 24
  5. lastUserAction : new Date()
  6. constructor: (@ide, @$scope) ->
  7. if !io?
  8. console.error "Socket.io javascript not loaded. Please check that the real-time service is running and accessible."
  9. @ide.socket =
  10. on: () ->
  11. $scope.$apply () =>
  12. @$scope.state.error = "Could not connect to websocket server :("
  13. return
  14. setInterval(() =>
  15. @disconnectIfInactive()
  16. , ONEHOUR)
  17. @userIsLeavingPage = false
  18. window.addEventListener 'beforeunload', =>
  19. @userIsLeavingPage = true
  20. return # Don't return true or it will show a pop up
  21. @connected = false
  22. @userIsInactive = false
  23. @$scope.connection =
  24. reconnecting: false
  25. # If we need to force everyone to reload the editor
  26. forced_disconnect: false
  27. inactive_disconnect: false
  28. @$scope.tryReconnectNow = () =>
  29. @tryReconnect()
  30. @$scope.$on 'cursor:editor:update', () =>
  31. @lastUserAction = new Date()
  32. if !@connected
  33. @tryReconnect()
  34. document.querySelector('body').addEventListener 'click', (e) =>
  35. if !@connected and e.target.id != 'try-reconnect-now-button'
  36. @tryReconnect()
  37. @ide.socket = io.connect null,
  38. reconnect: false
  39. 'connect timeout': 30 * 1000
  40. "force new connection": true
  41. @ide.socket.on "connect", () =>
  42. @connected = true
  43. @ide.pushEvent("connected")
  44. @$scope.$apply () =>
  45. @$scope.connection.reconnecting = false
  46. @$scope.connection.inactive_disconnect = false
  47. if @$scope.state.loading
  48. @$scope.state.load_progress = 70
  49. setTimeout(() =>
  50. @joinProject()
  51. , 100)
  52. @ide.socket.on "connect_failed", () =>
  53. @connected = false
  54. $scope.$apply () =>
  55. @$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."
  56. @ide.socket.on 'disconnect', () =>
  57. @connected = false
  58. @ide.pushEvent("disconnected")
  59. @$scope.$apply () =>
  60. @$scope.connection.reconnecting = false
  61. if !$scope.connection.forced_disconnect and !@userIsInactive
  62. @startAutoReconnectCountdown()
  63. @ide.socket.on 'forceDisconnect', (message) =>
  64. @$scope.$apply () =>
  65. @$scope.permissions.write = false
  66. @$scope.connection.forced_disconnect = true
  67. @ide.socket.disconnect()
  68. @ide.showGenericMessageModal("Please Refresh", """
  69. We're performing maintenance on ShareLaTeX and you need to refresh the editor.
  70. Sorry for any inconvenience.
  71. The editor will refresh in automatically in 10 seconds.
  72. """)
  73. setTimeout () ->
  74. location.reload()
  75. , 10 * 1000
  76. joinProject: () ->
  77. @ide.socket.emit 'joinProject', {
  78. project_id: @ide.project_id
  79. }, (err, project, permissionsLevel, protocolVersion) =>
  80. if @$scope.protocolVersion? and @$scope.protocolVersion != protocolVersion
  81. location.reload(true)
  82. @$scope.$apply () =>
  83. @$scope.protocolVersion = protocolVersion
  84. @$scope.project = project
  85. @$scope.permissionsLevel = permissionsLevel
  86. @$scope.state.load_progress = 100
  87. @$scope.state.loading = false
  88. @$scope.$broadcast "project:joined"
  89. reconnectImmediately: () ->
  90. @disconnect()
  91. @tryReconnect()
  92. disconnect: () ->
  93. @ide.socket.disconnect()
  94. startAutoReconnectCountdown: () ->
  95. twoMinutes = 2 * 60 * 1000
  96. if @lastUpdated? and new Date() - @lastUpdated > twoMinutes
  97. # between 1 minute and 3 minutes
  98. countdown = 60 + Math.floor(Math.random() * 120)
  99. else
  100. countdown = 3 + Math.floor(Math.random() * 7)
  101. if @userIsLeavingPage #user will have pressed refresh or back etc
  102. return
  103. @$scope.$apply () =>
  104. @$scope.connection.reconnecting = false
  105. @$scope.connection.reconnection_countdown = countdown
  106. setTimeout(=>
  107. if !@connected
  108. @timeoutId = setTimeout (=> @decreaseCountdown()), 1000
  109. , 200)
  110. cancelReconnect: () ->
  111. clearTimeout @timeoutId if @timeoutId?
  112. decreaseCountdown: () ->
  113. return if !@$scope.connection.reconnection_countdown?
  114. @$scope.$apply () =>
  115. @$scope.connection.reconnection_countdown--
  116. if @$scope.connection.reconnection_countdown <= 0
  117. @$scope.$apply () =>
  118. @tryReconnect()
  119. else
  120. @timeoutId = setTimeout (=> @decreaseCountdown()), 1000
  121. tryReconnect: () ->
  122. @cancelReconnect()
  123. delete @$scope.connection.reconnection_countdown
  124. return if @connected
  125. @$scope.connection.reconnecting = true
  126. @ide.socket.socket.reconnect()
  127. setTimeout (=> @startAutoReconnectCountdown() if !@connected), 2000
  128. disconnectIfInactive: ()->
  129. @userIsInactive = (new Date() - @lastUserAction) > @disconnectAfterMs
  130. if @userIsInactive and @connected
  131. @disconnect()
  132. @$scope.$apply () =>
  133. @$scope.connection.inactive_disconnect = true