|
|
@ -9,7 +9,8 @@ window.Conn = class TermConnection extends EventEmitter { |
|
|
|
this.pingInterval = null |
|
|
|
this.pingInterval = null |
|
|
|
this.xoff = false |
|
|
|
this.xoff = false |
|
|
|
this.autoXoffTimeout = null |
|
|
|
this.autoXoffTimeout = null |
|
|
|
this.reconTimeout = null |
|
|
|
this.reconnTimeout = null |
|
|
|
|
|
|
|
this.forceClosing = false |
|
|
|
|
|
|
|
|
|
|
|
this.pageShown = false |
|
|
|
this.pageShown = false |
|
|
|
} |
|
|
|
} |
|
|
@ -19,22 +20,26 @@ window.Conn = class TermConnection extends EventEmitter { |
|
|
|
this.heartbeat() |
|
|
|
this.heartbeat() |
|
|
|
this.send('i') |
|
|
|
this.send('i') |
|
|
|
|
|
|
|
|
|
|
|
this.emit('open') |
|
|
|
this.emit('connect') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onWSClose (evt) { |
|
|
|
onWSClose (evt) { |
|
|
|
|
|
|
|
if (this.forceClosing) return |
|
|
|
console.warn('SOCKET CLOSED, code ' + evt.code + '. Reconnecting...') |
|
|
|
console.warn('SOCKET CLOSED, code ' + evt.code + '. Reconnecting...') |
|
|
|
clearTimeout(this.reconTimeout) |
|
|
|
if (evt.code < 1000) { |
|
|
|
this.reconTimeout = setTimeout(() => this.init(), 2000) |
|
|
|
console.error('Bad code from socket!') |
|
|
|
// this happens when the buffer gets fucked up via invalid unicode.
|
|
|
|
// this sometimes happens for unknown reasons, code < 1000 is invalid
|
|
|
|
// we basically use polling instead of socket then
|
|
|
|
location.reload() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.emit('close', evt.code) |
|
|
|
clearTimeout(this.reconnTimeout) |
|
|
|
|
|
|
|
this.reconnTimeout = setTimeout(() => this.init(), 2000) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.emit('disconnect', evt.code) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onWSMessage (evt) { |
|
|
|
onWSMessage (evt) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
// . = heartbeat
|
|
|
|
|
|
|
|
switch (evt.data.charAt(0)) { |
|
|
|
switch (evt.data.charAt(0)) { |
|
|
|
case '.': |
|
|
|
case '.': |
|
|
|
// heartbeat, no-op message
|
|
|
|
// heartbeat, no-op message
|
|
|
@ -99,10 +104,20 @@ window.Conn = class TermConnection extends EventEmitter { |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Safely close the socket */ |
|
|
|
|
|
|
|
closeSocket () { |
|
|
|
|
|
|
|
if (this.ws) { |
|
|
|
|
|
|
|
this.forceClosing = true |
|
|
|
|
|
|
|
this.ws.close() |
|
|
|
|
|
|
|
this.forceClosing = false |
|
|
|
|
|
|
|
this.ws = null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
init () { |
|
|
|
init () { |
|
|
|
if (window._demo) { |
|
|
|
if (window._demo) { |
|
|
|
if (typeof window.demoInterface === 'undefined') { |
|
|
|
if (typeof window.demoInterface === 'undefined') { |
|
|
|
alert('Demoing non-demo demo!') // this will catch mistakes when deploying to the website
|
|
|
|
alert('Demoing non-demo build!') // this will catch mistakes when deploying to the website
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
demoInterface.init(screen) |
|
|
|
demoInterface.init(screen) |
|
|
|
showPage() |
|
|
|
showPage() |
|
|
@ -110,9 +125,11 @@ window.Conn = class TermConnection extends EventEmitter { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
clearTimeout(this.reconTimeout) |
|
|
|
clearTimeout(this.reconnTimeout) |
|
|
|
clearTimeout(this.heartbeatTimeout) |
|
|
|
clearTimeout(this.heartbeatTimeout) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.closeSocket() |
|
|
|
|
|
|
|
|
|
|
|
this.ws = new WebSocket('ws://' + _root + '/term/update.ws') |
|
|
|
this.ws = new WebSocket('ws://' + _root + '/term/update.ws') |
|
|
|
this.ws.addEventListener('open', (...args) => this.onWSOpen(...args)) |
|
|
|
this.ws.addEventListener('open', (...args) => this.onWSOpen(...args)) |
|
|
|
this.ws.addEventListener('close', (...args) => this.onWSClose(...args)) |
|
|
|
this.ws.addEventListener('close', (...args) => this.onWSClose(...args)) |
|
|
@ -120,7 +137,7 @@ window.Conn = class TermConnection extends EventEmitter { |
|
|
|
console.log('Opening socket.') |
|
|
|
console.log('Opening socket.') |
|
|
|
this.heartbeat() |
|
|
|
this.heartbeat() |
|
|
|
|
|
|
|
|
|
|
|
this.emit('connect') |
|
|
|
this.emit('open') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
heartbeat () { |
|
|
|
heartbeat () { |
|
|
@ -129,20 +146,25 @@ window.Conn = class TermConnection extends EventEmitter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onHeartbeatFail () { |
|
|
|
onHeartbeatFail () { |
|
|
|
|
|
|
|
this.closeSocket() |
|
|
|
|
|
|
|
this.emit('silence') |
|
|
|
console.error('Heartbeat lost, probing server...') |
|
|
|
console.error('Heartbeat lost, probing server...') |
|
|
|
clearInterval(this.pingInterval) |
|
|
|
clearInterval(this.pingInterval) |
|
|
|
|
|
|
|
|
|
|
|
this.pingInterval = setInterval(() => { |
|
|
|
this.pingInterval = setInterval(() => { |
|
|
|
console.log('> ping') |
|
|
|
console.log('> ping') |
|
|
|
this.emit('ping') |
|
|
|
this.emit('ping') |
|
|
|
$.get('http://' + _root + '/system/ping', (resp, status) => { |
|
|
|
$.get('http://' + _root + '/system/ping', (resp, status) => { |
|
|
|
if (status === 200) { |
|
|
|
if (status === 200) { |
|
|
|
clearInterval(this.pingInterval) |
|
|
|
clearInterval(this.pingInterval) |
|
|
|
console.info('Server ready, reloading page...') |
|
|
|
console.info('Server ready, opening socket…') |
|
|
|
this.emit('ping-success') |
|
|
|
this.emit('ping-success') |
|
|
|
location.reload() |
|
|
|
this.init() |
|
|
|
|
|
|
|
// location.reload()
|
|
|
|
} else this.emit('ping-fail', status) |
|
|
|
} else this.emit('ping-fail', status) |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
timeout: 100 |
|
|
|
timeout: 100, |
|
|
|
|
|
|
|
loader: false // we have loader on-screen
|
|
|
|
}) |
|
|
|
}) |
|
|
|
}, 1000) |
|
|
|
}, 1000) |
|
|
|
} |
|
|
|
} |
|
|
|