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