some reconn timing adjustments

box-drawing
Ondřej Hruška 7 years ago
parent 5eec2f523b
commit 8d2d5fda42
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 37
      js/term/connection.js

@ -3,6 +3,9 @@ const $ = require('../lib/chibi')
let demo
try { demo = require('./demo') } catch (err) {}
const RECONN_DELAY = 2000
const HEARTBEAT_TIME = 3000
/** Handle connections */
module.exports = class TermConnection extends EventEmitter {
constructor (screen) {
@ -17,6 +20,7 @@ module.exports = class TermConnection extends EventEmitter {
this.reconnTimeout = null
this.forceClosing = false
try {
this.blobReader = new FileReader()
this.blobReader.onload = (evt) => {
this.onDecodedWSMessage(this.blobReader.result)
@ -24,6 +28,9 @@ module.exports = class TermConnection extends EventEmitter {
this.blobReader.onerror = (evt) => {
console.error(evt)
}
} catch (e) {
this.blobReader = null
}
this.pageShown = false
@ -67,7 +74,7 @@ module.exports = class TermConnection extends EventEmitter {
}
clearTimeout(this.reconnTimeout)
this.reconnTimeout = setTimeout(() => this.init(), 2000)
this.reconnTimeout = setTimeout(() => this.init(), RECONN_DELAY)
this.emit('disconnect', evt.code)
}
@ -75,6 +82,7 @@ module.exports = class TermConnection extends EventEmitter {
onDecodedWSMessage (str) {
switch (str.charAt(0)) {
case '.':
console.log(str)
// heartbeat, no-op message
break
@ -106,6 +114,11 @@ module.exports = class TermConnection extends EventEmitter {
onWSMessage (evt) {
if (typeof evt.data === 'string') this.onDecodedWSMessage(evt.data)
else {
if (!this.blobReader) {
console.error('No FileReader!')
return
}
if (this.blobReader.readyState !== 1) {
this.blobReader.readAsText(evt.data)
} else {
@ -183,16 +196,10 @@ module.exports = class TermConnection extends EventEmitter {
heartbeat () {
clearTimeout(this.heartbeatTimeout)
this.heartbeatTimeout = setTimeout(() => this.onHeartbeatFail(), 2500)
this.heartbeatTimeout = setTimeout(() => this.onHeartbeatFail(), HEARTBEAT_TIME)
}
onHeartbeatFail () {
this.closeSocket()
this.emit('silence')
console.error('Heartbeat lost, probing server...')
clearInterval(this.pingInterval)
this.pingInterval = setInterval(() => {
sendPing () {
console.log('> ping')
this.emit('ping')
$.get('http://' + window._root + '/system/ping', (resp, status) => {
@ -207,6 +214,16 @@ module.exports = class TermConnection extends EventEmitter {
timeout: 100,
loader: false // we have loader on-screen
})
}, 1000)
}
onHeartbeatFail () {
this.closeSocket()
this.emit('silence')
console.error('Heartbeat lost, probing server...')
clearInterval(this.pingInterval)
this.pingInterval = setInterval(() => { this.sendPing() }, 1000)
// first ping, if this gets through, it'll will reduce delay
setTimeout(() => { this.sendPing() }, 200)
}
}

Loading…
Cancel
Save