ConnectionManager.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Fix any style issues and re-enable lint.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS206: Consider reworking classes to avoid initClass
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. /* global io */
  11. import SocketIoShim from './SocketIoShim'
  12. let ConnectionManager
  13. const ONEHOUR = 1000 * 60 * 60
  14. export default ConnectionManager = (function() {
  15. ConnectionManager = class ConnectionManager {
  16. static initClass() {
  17. this.prototype.disconnectAfterMs = ONEHOUR * 24
  18. this.prototype.lastUserAction = new Date()
  19. this.prototype.MIN_RETRY_INTERVAL = 1000 // ms, rate limit on reconnects for user clicking "try now"
  20. this.prototype.BACKGROUND_RETRY_INTERVAL = 5 * 1000
  21. this.prototype.JOIN_PROJECT_RETRY_INTERVAL = 5000
  22. this.prototype.JOIN_PROJECT_MAX_RETRY_INTERVAL = 60000
  23. this.prototype.RECONNECT_GRACEFULLY_RETRY_INTERVAL = 5000 // ms
  24. this.prototype.MAX_RECONNECT_GRACEFULLY_INTERVAL = 45 * 1000
  25. }
  26. constructor(ide, $scope) {
  27. this.ide = ide
  28. this.$scope = $scope
  29. this.wsUrl = ide.wsUrl || null // websocket url (if defined)
  30. if (typeof io === 'undefined' || io === null) {
  31. if (this.wsUrl && !window.location.href.match(/ws=fallback/)) {
  32. // if we tried to boot from a custom real-time backend and failed,
  33. // try reloading and falling back to the siteUrl
  34. window.location = window.location.href + '?ws=fallback'
  35. }
  36. console.error(
  37. 'Socket.io javascript not loaded. Please check that the real-time service is running and accessible.'
  38. )
  39. this.ide.socket = SocketIoShim.stub()
  40. this.$scope.$apply(() => {
  41. return (this.$scope.state.error =
  42. 'Could not connect to websocket server :(')
  43. })
  44. return
  45. }
  46. setInterval(() => {
  47. return this.disconnectIfInactive()
  48. }, ONEHOUR)
  49. // trigger a reconnect immediately if network comes back online
  50. window.addEventListener('online', () => {
  51. sl_console.log('[online] browser notified online')
  52. if (!this.connected) {
  53. return this.tryReconnectWithRateLimit({ force: true })
  54. }
  55. })
  56. this.userIsLeavingPage = false
  57. window.addEventListener('beforeunload', () => {
  58. this.userIsLeavingPage = true
  59. }) // Don't return true or it will show a pop up
  60. this.connected = false
  61. this.userIsInactive = false
  62. this.gracefullyReconnecting = false
  63. this.shuttingDown = false
  64. this.joinProjectRetryInterval = this.JOIN_PROJECT_RETRY_INTERVAL
  65. this.$scope.connection = {
  66. debug: sl_debugging,
  67. reconnecting: false,
  68. stillReconnecting: false,
  69. // If we need to force everyone to reload the editor
  70. forced_disconnect: false,
  71. inactive_disconnect: false,
  72. jobId: 0
  73. }
  74. this.$scope.tryReconnectNow = () => {
  75. // user manually requested reconnection via "Try now" button
  76. return this.tryReconnectWithRateLimit({ force: true })
  77. }
  78. this.$scope.$on('cursor:editor:update', () => {
  79. this.lastUserAction = new Date() // time of last edit
  80. if (!this.connected) {
  81. // user is editing, try to reconnect
  82. return this.tryReconnectWithRateLimit()
  83. }
  84. })
  85. document.querySelector('body').addEventListener('click', e => {
  86. if (
  87. !this.shuttingDown &&
  88. !this.connected &&
  89. e.target.id !== 'try-reconnect-now-button'
  90. ) {
  91. // user is editing, try to reconnect
  92. return this.tryReconnectWithRateLimit()
  93. }
  94. })
  95. // initial connection attempt
  96. this.updateConnectionManagerState('connecting')
  97. let parsedURL
  98. try {
  99. parsedURL = new URL(this.wsUrl || '/socket.io', window.location)
  100. } catch (e) {
  101. // hello IE11
  102. if (
  103. this.wsUrl &&
  104. this.wsUrl.indexOf('/') !== 0 &&
  105. !window.location.href.match(/ws=fallback/)
  106. ) {
  107. // do not even try parsing the wsUrl, use the fallback
  108. window.location = window.location.href + '?ws=fallback'
  109. }
  110. parsedURL = {
  111. origin: null,
  112. pathname: this.wsUrl || '/socket.io'
  113. }
  114. }
  115. this.ide.socket = SocketIoShim.connect(parsedURL.origin, {
  116. resource: parsedURL.pathname.slice(1),
  117. reconnect: false,
  118. 'connect timeout': 30 * 1000,
  119. 'force new connection': true
  120. })
  121. // handle network-level websocket errors (e.g. failed dns lookups)
  122. let connectionAttempt = 1
  123. let connectionErrorHandler = err => {
  124. if (
  125. window.wsRetryHandshake &&
  126. connectionAttempt++ < window.wsRetryHandshake
  127. ) {
  128. return setTimeout(() => this.ide.socket.socket.connect(), 100)
  129. }
  130. this.updateConnectionManagerState('error')
  131. sl_console.log('socket.io error', err)
  132. if (this.wsUrl && !window.location.href.match(/ws=fallback/)) {
  133. // if we tried to load a custom websocket location and failed
  134. // try reloading and falling back to the siteUrl
  135. window.location = window.location.href + '?ws=fallback'
  136. } else {
  137. this.connected = false
  138. return this.$scope.$apply(() => {
  139. return (this.$scope.state.error =
  140. "Unable to connect, please view the <u><a href='/learn/Kb/Connection_problems'>connection problems guide</a></u> to fix the issue.")
  141. })
  142. }
  143. }
  144. this.ide.socket.on('error', connectionErrorHandler)
  145. // The "connect" event is the first event we get back. It only
  146. // indicates that the websocket is connected, we still need to
  147. // pass authentication to join a project.
  148. this.ide.socket.on('connect', () => {
  149. // state should be 'connecting'...
  150. // remove connection error handler when connected, avoid unwanted fallbacks
  151. this.ide.socket.removeListener('error', connectionErrorHandler)
  152. sl_console.log('[socket.io connect] Connected')
  153. this.updateConnectionManagerState('authenticating')
  154. })
  155. // The next event we should get is an authentication response
  156. // from the server, either "connectionAccepted" or
  157. // "connectionRejected".
  158. this.ide.socket.on('connectionAccepted', (_, publicId) => {
  159. this.ide.socket.publicId = publicId || this.ide.socket.socket.sessionid
  160. // state should be 'authenticating'...
  161. sl_console.log('[socket.io connectionAccepted] allowed to connect')
  162. this.connected = true
  163. this.gracefullyReconnecting = false
  164. this.ide.pushEvent('connected')
  165. this.updateConnectionManagerState('joining')
  166. this.$scope.$apply(() => {
  167. if (this.$scope.state.loading) {
  168. this.$scope.state.load_progress = 70
  169. }
  170. })
  171. // we have passed authentication so we can now join the project
  172. let connectionJobId = this.$scope.connection.jobId
  173. setTimeout(() => {
  174. this.joinProject(connectionJobId)
  175. }, 100)
  176. })
  177. this.ide.socket.on('connectionRejected', err => {
  178. // state should be 'authenticating'...
  179. sl_console.log(
  180. '[socket.io connectionRejected] session not valid or other connection error'
  181. )
  182. // real time sends a 'retry' message if the process was shutting down
  183. if (err && err.message === 'retry') {
  184. return this.tryReconnectWithRateLimit()
  185. }
  186. // we have failed authentication, usually due to an invalid session cookie
  187. return this.reportConnectionError(err)
  188. })
  189. // Alternatively the attempt to connect can fail completely, so
  190. // we never get into the "connect" state.
  191. this.ide.socket.on('connect_failed', () => {
  192. this.updateConnectionManagerState('error')
  193. this.connected = false
  194. return this.$scope.$apply(() => {
  195. return (this.$scope.state.error =
  196. "Unable to connect, please view the <u><a href='/learn/Kb/Connection_problems'>connection problems guide</a></u> to fix the issue.")
  197. })
  198. })
  199. // We can get a "disconnect" event at any point after the
  200. // "connect" event.
  201. this.ide.socket.on('disconnect', () => {
  202. sl_console.log('[socket.io disconnect] Disconnected')
  203. this.connected = false
  204. this.ide.pushEvent('disconnected')
  205. if (!this.$scope.connection.state.match(/^waiting/)) {
  206. if (
  207. !this.$scope.connection.forced_disconnect &&
  208. !this.userIsInactive &&
  209. !this.shuttingDown
  210. ) {
  211. this.startAutoReconnectCountdown()
  212. } else {
  213. this.updateConnectionManagerState('inactive')
  214. }
  215. }
  216. })
  217. // Site administrators can send the forceDisconnect event to all users
  218. this.ide.socket.on('forceDisconnect', message => {
  219. this.updateConnectionManagerState('inactive')
  220. this.$scope.$apply(() => {
  221. this.$scope.permissions.write = false
  222. return (this.$scope.connection.forced_disconnect = true)
  223. })
  224. this.ide.socket.disconnect()
  225. this.ide.showGenericMessageModal(
  226. 'Please Refresh',
  227. `\
  228. We're performing maintenance on Overleaf and you need to refresh the editor.
  229. Sorry for any inconvenience.
  230. The editor will refresh in automatically in 10 seconds.\
  231. `
  232. )
  233. return setTimeout(() => location.reload(), 10 * 1000)
  234. })
  235. this.ide.socket.on('reconnectGracefully', () => {
  236. sl_console.log('Reconnect gracefully')
  237. this.reconnectGracefully()
  238. })
  239. }
  240. updateConnectionManagerState(state) {
  241. this.$scope.$apply(() => {
  242. this.$scope.connection.jobId += 1
  243. let jobId = this.$scope.connection.jobId
  244. sl_console.log(
  245. `[updateConnectionManagerState ${jobId}] from ${this.$scope.connection.state} to ${state}`
  246. )
  247. this.$scope.connection.state = state
  248. this.$scope.connection.reconnecting = false
  249. this.$scope.connection.stillReconnecting = false
  250. this.$scope.connection.inactive_disconnect = false
  251. this.$scope.connection.joining = false
  252. this.$scope.connection.reconnection_countdown = null
  253. if (state === 'connecting') {
  254. // initial connection
  255. } else if (state === 'reconnecting') {
  256. // reconnection after a connection has failed
  257. this.stopReconnectCountdownTimer()
  258. this.$scope.connection.reconnecting = true
  259. // if reconnecting takes more than 1s (it doesn't, usually) show the
  260. // 'reconnecting...' warning
  261. setTimeout(() => {
  262. if (
  263. this.$scope.connection.reconnecting &&
  264. this.$scope.connection.jobId === jobId
  265. ) {
  266. this.$scope.connection.stillReconnecting = true
  267. }
  268. }, 1000)
  269. } else if (state === 'reconnectFailed') {
  270. // reconnect attempt failed
  271. } else if (state === 'authenticating') {
  272. // socket connection has been established, trying to authenticate
  273. } else if (state === 'joining') {
  274. // authenticated, joining project
  275. this.$scope.connection.joining = true
  276. } else if (state === 'ready') {
  277. // project has been joined
  278. } else if (state === 'waitingCountdown') {
  279. // disconnected and waiting to reconnect via the countdown timer
  280. this.stopReconnectCountdownTimer()
  281. } else if (state === 'waitingGracefully') {
  282. // disconnected and waiting to reconnect gracefully
  283. this.stopReconnectCountdownTimer()
  284. } else if (state === 'inactive') {
  285. // disconnected and not trying to reconnect (inactive)
  286. } else if (state === 'error') {
  287. // something is wrong
  288. } else {
  289. sl_console.log(
  290. `[WARN] [updateConnectionManagerState ${jobId}] got unrecognised state ${state}`
  291. )
  292. }
  293. })
  294. }
  295. expectConnectionManagerState(state, jobId) {
  296. if (
  297. this.$scope.connection.state === state &&
  298. (!jobId || jobId === this.$scope.connection.jobId)
  299. ) {
  300. return true
  301. }
  302. sl_console.log(
  303. `[WARN] [state mismatch] expected state ${state}${
  304. jobId ? '/' + jobId : ''
  305. } when in ${this.$scope.connection.state}/${
  306. this.$scope.connection.jobId
  307. }`
  308. )
  309. return false
  310. }
  311. // Error reporting, which can reload the page if appropriate
  312. reportConnectionError(err) {
  313. sl_console.log('[socket.io] reporting connection error')
  314. this.updateConnectionManagerState('error')
  315. if (
  316. (err != null ? err.message : undefined) === 'not authorized' ||
  317. (err != null ? err.message : undefined) === 'invalid session'
  318. ) {
  319. return (window.location = `/login?redir=${encodeURI(
  320. window.location.pathname
  321. )}`)
  322. } else {
  323. this.ide.socket.disconnect()
  324. return this.ide.showGenericMessageModal(
  325. 'Something went wrong connecting',
  326. `\
  327. Something went wrong connecting to your project. Please refresh if this continues to happen.\
  328. `
  329. )
  330. }
  331. }
  332. joinProject(connectionId) {
  333. sl_console.log(`[joinProject ${connectionId}] joining...`)
  334. // Note: if the "joinProject" message doesn't reach the server
  335. // (e.g. if we are in a disconnected state at this point) the
  336. // callback will never be executed
  337. if (!this.expectConnectionManagerState('joining', connectionId)) {
  338. sl_console.log(
  339. `[joinProject ${connectionId}] aborting with stale connection`
  340. )
  341. return
  342. }
  343. const data = {
  344. project_id: this.ide.project_id
  345. }
  346. if (window.anonymousAccessToken) {
  347. data.anonymousAccessToken = window.anonymousAccessToken
  348. }
  349. this.ide.socket.emit(
  350. 'joinProject',
  351. data,
  352. (err, project, permissionsLevel, protocolVersion) => {
  353. if (err != null || project == null) {
  354. err = err || {}
  355. if (err.code === 'ProjectNotFound') {
  356. // A stale browser tab tried to join a deleted project.
  357. // Reloading the page will render a 404.
  358. this.ide
  359. .showGenericMessageModal(
  360. 'Project has been deleted',
  361. 'This project has been deleted by the owner.'
  362. )
  363. .result.then(() => location.reload(true))
  364. return
  365. }
  366. if (err.code === 'TooManyRequests') {
  367. sl_console.log(
  368. `[joinProject ${connectionId}] retrying: ${err.message}`
  369. )
  370. setTimeout(
  371. () => this.joinProject(connectionId),
  372. this.joinProjectRetryInterval
  373. )
  374. if (
  375. this.joinProjectRetryInterval <
  376. this.JOIN_PROJECT_MAX_RETRY_INTERVAL
  377. ) {
  378. this.joinProjectRetryInterval += this.JOIN_PROJECT_RETRY_INTERVAL
  379. }
  380. return
  381. } else {
  382. return this.reportConnectionError(err)
  383. }
  384. }
  385. this.joinProjectRetryInterval = this.JOIN_PROJECT_RETRY_INTERVAL
  386. if (
  387. this.$scope.protocolVersion != null &&
  388. this.$scope.protocolVersion !== protocolVersion
  389. ) {
  390. location.reload(true)
  391. }
  392. this.$scope.$apply(() => {
  393. this.updateConnectionManagerState('ready')
  394. this.$scope.protocolVersion = protocolVersion
  395. const defaultProjectAttributes = { rootDoc_id: null }
  396. this.$scope.project = { ...defaultProjectAttributes, ...project }
  397. this.$scope.permissionsLevel = permissionsLevel
  398. this.ide.loadingManager.socketLoaded()
  399. this.$scope.$broadcast('project:joined')
  400. })
  401. }
  402. )
  403. }
  404. reconnectImmediately() {
  405. this.disconnect()
  406. return this.tryReconnect()
  407. }
  408. disconnect(options) {
  409. if (options && options.permanent) {
  410. sl_console.log('[disconnect] shutting down ConnectionManager')
  411. this.updateConnectionManagerState('inactive')
  412. this.shuttingDown = true // prevent reconnection attempts
  413. } else if (this.ide.socket.socket && !this.ide.socket.socket.connected) {
  414. sl_console.log(
  415. '[socket.io] skipping disconnect because socket.io has not connected'
  416. )
  417. return
  418. }
  419. sl_console.log('[socket.io] disconnecting client')
  420. return this.ide.socket.disconnect()
  421. }
  422. startAutoReconnectCountdown() {
  423. this.updateConnectionManagerState('waitingCountdown')
  424. let connectionId = this.$scope.connection.jobId
  425. let countdown
  426. sl_console.log('[ConnectionManager] starting autoreconnect countdown')
  427. const twoMinutes = 2 * 60 * 1000
  428. if (
  429. this.lastUserAction != null &&
  430. new Date() - this.lastUserAction > twoMinutes
  431. ) {
  432. // between 1 minute and 3 minutes
  433. countdown = 60 + Math.floor(Math.random() * 120)
  434. } else {
  435. countdown = 3 + Math.floor(Math.random() * 7)
  436. }
  437. if (this.userIsLeavingPage) {
  438. // user will have pressed refresh or back etc
  439. return
  440. }
  441. this.$scope.$apply(() => {
  442. this.$scope.connection.reconnecting = false
  443. this.$scope.connection.stillReconnecting = false
  444. this.$scope.connection.joining = false
  445. this.$scope.connection.reconnection_countdown = countdown
  446. })
  447. setTimeout(() => {
  448. if (!this.connected && !this.countdownTimeoutId) {
  449. this.countdownTimeoutId = setTimeout(
  450. () => this.decreaseCountdown(connectionId),
  451. 1000
  452. )
  453. }
  454. }, 200)
  455. }
  456. stopReconnectCountdownTimer() {
  457. // clear timeout and set to null so we know there is no countdown running
  458. if (this.countdownTimeoutId != null) {
  459. sl_console.log(
  460. '[ConnectionManager] cancelling existing reconnect timer'
  461. )
  462. clearTimeout(this.countdownTimeoutId)
  463. this.countdownTimeoutId = null
  464. }
  465. }
  466. decreaseCountdown(connectionId) {
  467. this.countdownTimeoutId = null
  468. if (this.$scope.connection.reconnection_countdown == null) {
  469. return
  470. }
  471. if (
  472. !this.expectConnectionManagerState('waitingCountdown', connectionId)
  473. ) {
  474. sl_console.log(
  475. `[ConnectionManager] Aborting stale countdown ${connectionId}`
  476. )
  477. return
  478. }
  479. sl_console.log(
  480. '[ConnectionManager] decreasing countdown',
  481. this.$scope.connection.reconnection_countdown
  482. )
  483. this.$scope.$apply(() => {
  484. this.$scope.connection.reconnection_countdown--
  485. })
  486. if (this.$scope.connection.reconnection_countdown <= 0) {
  487. this.$scope.connection.reconnecting = false
  488. this.$scope.$apply(() => {
  489. this.tryReconnect()
  490. })
  491. } else {
  492. this.countdownTimeoutId = setTimeout(
  493. () => this.decreaseCountdown(connectionId),
  494. 1000
  495. )
  496. }
  497. }
  498. tryReconnect() {
  499. sl_console.log('[ConnectionManager] tryReconnect')
  500. if (
  501. this.connected ||
  502. this.shuttingDown ||
  503. this.$scope.connection.reconnecting
  504. ) {
  505. return
  506. }
  507. this.updateConnectionManagerState('reconnecting')
  508. sl_console.log('[ConnectionManager] Starting new connection')
  509. const removeHandler = () => {
  510. this.ide.socket.removeListener('error', handleFailure)
  511. this.ide.socket.removeListener('connect', handleSuccess)
  512. }
  513. const handleFailure = () => {
  514. sl_console.log('[ConnectionManager] tryReconnect: failed')
  515. removeHandler()
  516. this.updateConnectionManagerState('reconnectFailed')
  517. this.tryReconnectWithRateLimit({ force: true })
  518. }
  519. const handleSuccess = () => {
  520. sl_console.log('[ConnectionManager] tryReconnect: success')
  521. removeHandler()
  522. }
  523. this.ide.socket.on('error', handleFailure)
  524. this.ide.socket.on('connect', handleSuccess)
  525. // use socket.io connect() here to make a single attempt, the
  526. // reconnect() method makes multiple attempts
  527. this.ide.socket.socket.connect()
  528. // record the time of the last attempt to connect
  529. this.lastConnectionAttempt = new Date()
  530. }
  531. tryReconnectWithRateLimit(options) {
  532. // bail out if the reconnect is already in progress
  533. if (this.$scope.connection.reconnecting || this.connected) {
  534. return
  535. }
  536. // bail out if we are going to reconnect soon anyway
  537. const reconnectingSoon =
  538. this.$scope.connection.reconnection_countdown != null &&
  539. this.$scope.connection.reconnection_countdown <= 5
  540. const clickedTryNow = options != null ? options.force : undefined // user requested reconnection
  541. if (reconnectingSoon && !clickedTryNow) {
  542. return
  543. }
  544. // bail out if we tried reconnecting recently
  545. const allowedInterval = clickedTryNow
  546. ? this.MIN_RETRY_INTERVAL
  547. : this.BACKGROUND_RETRY_INTERVAL
  548. if (
  549. this.lastConnectionAttempt != null &&
  550. new Date() - this.lastConnectionAttempt < allowedInterval
  551. ) {
  552. if (this.$scope.connection.state !== 'waitingCountdown') {
  553. this.startAutoReconnectCountdown()
  554. }
  555. return
  556. }
  557. this.tryReconnect()
  558. }
  559. disconnectIfInactive() {
  560. this.userIsInactive =
  561. new Date() - this.lastUserAction > this.disconnectAfterMs
  562. if (this.userIsInactive && this.connected) {
  563. this.disconnect()
  564. return this.$scope.$apply(() => {
  565. return (this.$scope.connection.inactive_disconnect = true)
  566. }) // 5 minutes
  567. }
  568. }
  569. reconnectGracefully(force) {
  570. if (this.reconnectGracefullyStarted == null) {
  571. this.reconnectGracefullyStarted = new Date()
  572. } else {
  573. if (!force) {
  574. sl_console.log(
  575. '[reconnectGracefully] reconnection is already in process, so skipping'
  576. )
  577. return
  578. }
  579. }
  580. const userIsInactive =
  581. new Date() - this.lastUserAction >
  582. this.RECONNECT_GRACEFULLY_RETRY_INTERVAL
  583. const maxIntervalReached =
  584. new Date() - this.reconnectGracefullyStarted >
  585. this.MAX_RECONNECT_GRACEFULLY_INTERVAL
  586. if (userIsInactive || maxIntervalReached) {
  587. sl_console.log(
  588. "[reconnectGracefully] User didn't do anything for last 5 seconds, reconnecting"
  589. )
  590. this._reconnectGracefullyNow()
  591. } else {
  592. sl_console.log(
  593. '[reconnectGracefully] User is working, will try again in 5 seconds'
  594. )
  595. this.updateConnectionManagerState('waitingGracefully')
  596. setTimeout(() => {
  597. this.reconnectGracefully(true)
  598. }, this.RECONNECT_GRACEFULLY_RETRY_INTERVAL)
  599. }
  600. }
  601. _reconnectGracefullyNow() {
  602. this.gracefullyReconnecting = true
  603. this.reconnectGracefullyStarted = null
  604. // Clear cookie so we don't go to the same backend server
  605. $.cookie('SERVERID', '', { expires: -1, path: '/' })
  606. return this.reconnectImmediately()
  607. }
  608. }
  609. ConnectionManager.initClass()
  610. return ConnectionManager
  611. })()