gotty/resources/gotty.js

97 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2015-08-16 09:47:23 +00:00
(function() {
var httpsEnabled = window.location.protocol == "https:";
var args = window.location.search;
2015-08-16 09:47:23 +00:00
var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws';
var protocols = ["gotty"];
var autoReconnect = -1;
2015-08-16 09:47:23 +00:00
var openWs = function() {
var ws = new WebSocket(url, protocols);
2015-08-16 09:47:23 +00:00
var term;
2015-08-16 09:47:23 +00:00
2015-09-01 04:27:43 +00:00
var pingTimer;
ws.onopen = function(event) {
ws.send(JSON.stringify({ Arguments: args, AuthToken: gotty_auth_token,}));
2015-09-01 04:27:43 +00:00
pingTimer = setInterval(sendPing, 30 * 1000, ws);
hterm.defaultStorage = new lib.Storage.Local();
hterm.defaultStorage.clear();
2015-08-16 09:47:23 +00:00
term = new hterm.Terminal();
2015-08-16 09:47:23 +00:00
term.getPrefs().set("send-encoding", "raw");
term.onTerminalReady = function() {
var io = term.io.push();
io.onVTKeystroke = function(str) {
ws.send("0" + str);
};
2015-08-16 09:47:23 +00:00
io.sendString = io.onVTKeystroke;
2015-08-16 09:47:23 +00:00
io.onTerminalResize = function(columns, rows) {
ws.send(
2015-09-01 04:27:43 +00:00
"2" + JSON.stringify(
{
columns: columns,
rows: rows,
}
)
2015-08-16 09:47:23 +00:00
)
};
term.installKeyboard();
2015-08-16 09:47:23 +00:00
};
term.decorate(document.getElementById("terminal"));
2015-08-16 09:47:23 +00:00
};
ws.onmessage = function(event) {
data = event.data.slice(1);
switch(event.data[0]) {
case '0':
term.io.writeUTF8(window.atob(data));
break;
case '1':
2015-09-01 04:27:43 +00:00
// pong
break;
2015-09-01 04:27:43 +00:00
case '2':
term.setWindowTitle(data);
break;
2015-09-01 04:27:43 +00:00
case '3':
preferences = JSON.parse(data);
Object.keys(preferences).forEach(function(key) {
console.log("Setting " + key + ": " + preferences[key]);
term.getPrefs().set(key, preferences[key]);
});
break;
2015-09-01 04:27:43 +00:00
case '4':
autoReconnect = JSON.parse(data);
2015-09-01 04:27:43 +00:00
console.log("Enabling reconnect: " + autoReconnect + " seconds")
break;
}
2015-09-01 04:27:43 +00:00
};
ws.onclose = function(event) {
if (term) {
term.uninstallKeyboard();
term.io.showOverlay("Connection Closed", null);
}
2015-09-01 04:27:43 +00:00
clearInterval(pingTimer);
if (autoReconnect > 0) {
setTimeout(openWs, autoReconnect * 1000);
}
};
2015-08-16 09:47:23 +00:00
}
2015-09-01 04:27:43 +00:00
var sendPing = function(ws) {
ws.send("1");
2015-08-16 09:47:23 +00:00
}
2015-09-01 04:27:43 +00:00
openWs();
2015-08-16 09:47:23 +00:00
})()