You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
191 lines
4.6 KiB
191 lines
4.6 KiB
7 years ago
|
const EventEmitter = require('events')
|
||
7 years ago
|
const $ = require('../lib/chibi')
|
||
7 years ago
|
let demo
|
||
|
try { demo = require('./demo') } catch (err) {}
|
||
7 years ago
|
|
||
7 years ago
|
/** Handle connections */
|
||
7 years ago
|
module.exports = class TermConnection extends EventEmitter {
|
||
7 years ago
|
constructor (screen) {
|
||
|
super()
|
||
|
|
||
|
this.screen = screen
|
||
|
this.ws = null
|
||
|
this.heartbeatTimeout = null
|
||
|
this.pingInterval = null
|
||
|
this.xoff = false
|
||
|
this.autoXoffTimeout = null
|
||
7 years ago
|
this.reconnTimeout = null
|
||
|
this.forceClosing = false
|
||
7 years ago
|
|
||
|
this.pageShown = false
|
||
7 years ago
|
|
||
7 years ago
|
document.addEventListener('visibilitychange', () => {
|
||
|
if (document.hidden === true) {
|
||
|
console.info('Window lost focus, freeing socket')
|
||
|
this.closeSocket()
|
||
|
clearTimeout(this.heartbeatTimeout)
|
||
|
} else {
|
||
|
console.info('Window got focus, re-connecting')
|
||
|
this.init()
|
||
|
}
|
||
|
}, false)
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
onWSOpen (evt) {
|
||
7 years ago
|
console.log('CONNECTED')
|
||
7 years ago
|
this.heartbeat()
|
||
|
this.send('i')
|
||
7 years ago
|
this.forceClosing = false
|
||
7 years ago
|
|
||
7 years ago
|
this.emit('connect')
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
onWSClose (evt) {
|
||
7 years ago
|
if (this.forceClosing) {
|
||
|
this.forceClosing = false
|
||
|
return
|
||
|
}
|
||
7 years ago
|
console.warn('SOCKET CLOSED, code ' + evt.code + '. Reconnecting...')
|
||
7 years ago
|
if (evt.code < 1000) {
|
||
|
console.error('Bad code from socket!')
|
||
|
// this sometimes happens for unknown reasons, code < 1000 is invalid
|
||
7 years ago
|
// location.reload()
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
clearTimeout(this.reconnTimeout)
|
||
|
this.reconnTimeout = setTimeout(() => this.init(), 2000)
|
||
|
|
||
|
this.emit('disconnect', evt.code)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
onWSMessage (evt) {
|
||
7 years ago
|
try {
|
||
|
switch (evt.data.charAt(0)) {
|
||
7 years ago
|
case '.':
|
||
|
// heartbeat, no-op message
|
||
7 years ago
|
break
|
||
7 years ago
|
|
||
|
case '-':
|
||
7 years ago
|
// console.log('xoff');
|
||
7 years ago
|
this.xoff = true
|
||
|
this.autoXoffTimeout = setTimeout(() => {
|
||
|
this.xoff = false
|
||
7 years ago
|
}, 250)
|
||
|
break
|
||
7 years ago
|
|
||
|
case '+':
|
||
7 years ago
|
// console.log('xon');
|
||
7 years ago
|
this.xoff = false
|
||
|
clearTimeout(this.autoXoffTimeout)
|
||
7 years ago
|
break
|
||
7 years ago
|
|
||
|
default:
|
||
7 years ago
|
this.screen.load(evt.data)
|
||
|
if (!this.pageShown) {
|
||
7 years ago
|
showPage()
|
||
7 years ago
|
this.pageShown = true
|
||
7 years ago
|
}
|
||
|
break
|
||
7 years ago
|
}
|
||
7 years ago
|
this.heartbeat()
|
||
7 years ago
|
} catch (e) {
|
||
7 years ago
|
console.error(e)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
canSend () {
|
||
|
return !this.xoff
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
send (message) {
|
||
|
if (window._demo) {
|
||
|
if (typeof window.demoInterface !== 'undefined') {
|
||
7 years ago
|
demo.input(message)
|
||
7 years ago
|
} else {
|
||
|
console.log(`TX: ${JSON.stringify(message)}`)
|
||
|
}
|
||
7 years ago
|
return true // Simulate success
|
||
7 years ago
|
}
|
||
7 years ago
|
if (this.xoff) {
|
||
7 years ago
|
// TODO queue
|
||
7 years ago
|
console.log("Can't send, flood control.")
|
||
|
return false
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
if (!this.ws) return false // for dry testing
|
||
|
if (this.ws.readyState !== 1) {
|
||
7 years ago
|
console.error('Socket not ready')
|
||
|
return false
|
||
7 years ago
|
}
|
||
7 years ago
|
if (typeof message != 'string') {
|
||
|
message = JSON.stringify(message)
|
||
7 years ago
|
}
|
||
7 years ago
|
this.ws.send(message)
|
||
7 years ago
|
return true
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/** Safely close the socket */
|
||
|
closeSocket () {
|
||
|
if (this.ws) {
|
||
|
this.forceClosing = true
|
||
|
this.ws.close()
|
||
|
this.ws = null
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
init () {
|
||
7 years ago
|
if (window._demo) {
|
||
7 years ago
|
if (typeof window.demoInterface === 'undefined') {
|
||
7 years ago
|
window.alert('Demoing non-demo build!') // this will catch mistakes when deploying to the website
|
||
7 years ago
|
} else {
|
||
7 years ago
|
demo.init(this.screen)
|
||
7 years ago
|
showPage()
|
||
|
}
|
||
7 years ago
|
return
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
clearTimeout(this.reconnTimeout)
|
||
7 years ago
|
clearTimeout(this.heartbeatTimeout)
|
||
7 years ago
|
|
||
7 years ago
|
this.closeSocket()
|
||
|
|
||
7 years ago
|
this.ws = new window.WebSocket('ws://' + window._root + '/term/update.ws')
|
||
7 years ago
|
this.ws.addEventListener('open', (...args) => this.onWSOpen(...args))
|
||
|
this.ws.addEventListener('close', (...args) => this.onWSClose(...args))
|
||
|
this.ws.addEventListener('message', (...args) => this.onWSMessage(...args))
|
||
7 years ago
|
console.log('Opening socket.')
|
||
7 years ago
|
this.heartbeat()
|
||
|
|
||
7 years ago
|
this.emit('open')
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
heartbeat () {
|
||
|
clearTimeout(this.heartbeatTimeout)
|
||
|
this.heartbeatTimeout = setTimeout(() => this.onHeartbeatFail(), 2000)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
onHeartbeatFail () {
|
||
7 years ago
|
this.closeSocket()
|
||
|
this.emit('silence')
|
||
7 years ago
|
console.error('Heartbeat lost, probing server...')
|
||
7 years ago
|
clearInterval(this.pingInterval)
|
||
7 years ago
|
|
||
7 years ago
|
this.pingInterval = setInterval(() => {
|
||
7 years ago
|
console.log('> ping')
|
||
7 years ago
|
this.emit('ping')
|
||
7 years ago
|
$.get('http://' + window._root + '/system/ping', (resp, status) => {
|
||
7 years ago
|
if (status === 200) {
|
||
7 years ago
|
clearInterval(this.pingInterval)
|
||
7 years ago
|
console.info('Server ready, opening socket…')
|
||
7 years ago
|
this.emit('ping-success')
|
||
7 years ago
|
this.init()
|
||
|
// location.reload()
|
||
7 years ago
|
} else this.emit('ping-fail', status)
|
||
7 years ago
|
}, {
|
||
7 years ago
|
timeout: 100,
|
||
|
loader: false // we have loader on-screen
|
||
7 years ago
|
})
|
||
|
}, 1000)
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|