tera/public/index.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

Terminal.applyAddon(fit);
Terminal.applyAddon(attach);
Terminal.applyAddon(fullscreen);
var term = new Terminal({
cursorBlink: true,
screenKeys: true
});
term.open(document.getElementById('terminal'));
term.toggleFullScreen();
term.fit();
var cmd = ''
term.on('data', (data) => {
if (data == '\r') {
term.writeln(data)
if (cmd.startsWith('/')) {
args = cmd.split(' ');
cmd = args[0].substr(1);
switch (cmd) {
case "connect":
let address = args[1] || 'https://tera-ws.miaowoo.cc/web'
term.writeln(`Connect to ${address}...`)
newcon(address)
break;
case "disconnect":
socket.close();
break;
}
} else {
socket && socket.emit('webmsg', { cmd });
}
cmd = ''
return
}
cmd += data;
term.write(data)
});
let socket = undefined
function newcon(address) {
if (socket) {
socket.close()
}
socket = io(address, {
path: '/ws',
transports: ['websocket'],
autoConnect: false,
});
socket.on('connect', function() {
term.writeln('connect')
});
socket.on('message', function(data) {
if (typeof data === "string") {
term.writeln(data);
} else {
for (const line of JSON.stringify(data, undefined, 4).split('\n')) {
term.writeln(line);
}
}
});
socket.on('disconnect', function() {
term.reset();
term.writeln('disconnect')
});
socket.connect()
}
window.onresize = function() {
term.fit();
term.scrollToBottom();
};