gotty/resources/gotty.js

91 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-08-16 09:47:23 +00:00
(function() {
var httpsEnabled = window.location.protocol == "https:";
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
ws.onopen = function(event) {
ws.send(gotty_auth_token);
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(
"1" + 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.writeUTF16(data);
break;
case '1':
term.setWindowTitle(data);
break;
case '2':
preferences = JSON.parse(data);
Object.keys(preferences).forEach(function(key) {
console.log("Setting " + key + ": " + preferences[key]);
term.getPrefs().set(key, preferences[key]);
});
break;
case '3':
autoReconnect = JSON.parse(data);
break;
}
}
ws.onclose = function(event) {
if (term) {
term.uninstallKeyboard();
term.io.showOverlay("Connection Closed", null);
}
tryReconnect();
}
ws.onerror = function(error) {
tryReconnect();
2015-08-23 11:40:18 +00:00
}
2015-08-16 09:47:23 +00:00
}
openWs();
var tryReconnect = function() {
if (autoReconnect >= 0) {
setTimeout(openWs, autoReconnect * 1000);
}
2015-08-16 09:47:23 +00:00
}
})()