Paillasse/portail/public/js/console-reelle.js

67 lines
2.8 KiB
JavaScript
Raw Normal View History

// Vraie console série (étape 9) : se branche sur le proxy WebSocket Go, qui
// relaie nova-serialproxy. Contrairement à paillasse-terminal.js (shell
// simulé pour la vitrine), ceci parle à une VM réelle.
(function () {
var THEME = {
background: '#13233A', foreground: '#E8EDF5', cursor: '#FFD23F', cursorAccent: '#13233A',
selectionBackground: '#2B4A73', black: '#0D192B', red: '#FF7A66', green: '#7CF2C0', yellow: '#FFD23F',
blue: '#8DB4FF', magenta: '#FF6FB5', cyan: '#7CE3F2', white: '#E8EDF5',
brightBlack: '#6B7F99', brightRed: '#FF9A8A', brightGreen: '#7CF2C0', brightYellow: '#FFD23F',
brightBlue: '#A9C6FF', brightMagenta: '#FF8FC6', brightCyan: '#9CEBF7', brightWhite: '#FFFFFF'
};
function mount(el, o) {
o = o || {};
var lectureSeule = !!o.lectureSeule;
var term = new window.Terminal({
theme: THEME, fontFamily: '"IBM Plex Mono", ui-monospace, monospace', fontSize: o.fontSize || 14,
lineHeight: 1.25, cursorBlink: !lectureSeule, cursorStyle: 'block', scrollback: 2000,
disableStdin: lectureSeule
});
var fit = window.FitAddon ? new window.FitAddon.FitAddon() : null;
if (fit) term.loadAddon(fit);
term.open(el);
var fitNow = function () { try { fit && fit.fit(); } catch (e) {} };
var raf, doFit = function () { cancelAnimationFrame(raf); raf = requestAnimationFrame(fitNow); };
fitNow(); window.addEventListener('resize', doFit);
if (window.ResizeObserver) new ResizeObserver(doFit).observe(el);
term.write('\x1b[2mConnexion à la console…\x1b[0m\r\n');
var protocole = location.protocol === 'https:' ? 'wss://' : 'ws://';
var ws = new WebSocket(protocole + location.host + '/console/' + o.jeton);
ws.binaryType = 'arraybuffer';
ws.onopen = function () {
term.write('\x1b[2mConnecté.\x1b[0m\r\n');
// VM du stock chaud démarrée depuis longtemps : sa console série
// n'affiche plus rien jusqu'à la prochaine touche. Un retour chariot
// fait réapparaître l'invite de connexion.
if (o.reveil && !lectureSeule) ws.send('\r');
};
ws.onmessage = function (evenement) {
if (evenement.data instanceof ArrayBuffer) {
term.write(new Uint8Array(evenement.data));
} else {
term.write(evenement.data);
}
};
ws.onclose = function () { term.write('\r\n\x1b[2mConnexion fermée.\x1b[0m\r\n'); };
ws.onerror = function () { term.write('\r\n\x1b[31mErreur de connexion à la console.\x1b[0m\r\n'); };
if (!lectureSeule) {
term.onData(function (donnees) {
if (ws.readyState === WebSocket.OPEN) ws.send(donnees);
});
}
return { term: term, ws: ws };
}
window.SyllabusConsoleReelle = { mount: function (el, o) {
var go = function () { mount(el, o); };
(document.fonts && document.fonts.ready ? document.fonts.ready : Promise.resolve()).then(go);
} };
})();