2019-07-20 05:12:21 +00:00
|
|
|
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',
|
2019-07-24 05:03:06 +00:00
|
|
|
transports: ['websocket'],
|
|
|
|
autoConnect: false,
|
2019-07-20 05:12:21 +00:00
|
|
|
});
|
|
|
|
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();
|
|
|
|
};
|