2015-08-16 09:47:23 +00:00
|
|
|
(function() {
|
|
|
|
var httpsEnabled = window.location.protocol == "https:";
|
2015-10-05 07:50:13 +00:00
|
|
|
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"];
|
2015-08-23 22:14:24 +00:00
|
|
|
var autoReconnect = -1;
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
var openWs = function() {
|
|
|
|
var ws = new WebSocket(url, protocols);
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
var term;
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-09-01 04:27:43 +00:00
|
|
|
var pingTimer;
|
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
ws.onopen = function(event) {
|
2015-10-05 07:50:13 +00:00
|
|
|
ws.send(JSON.stringify({ Arguments: args, AuthToken: gotty_auth_token,}));
|
2015-09-01 04:27:43 +00:00
|
|
|
pingTimer = setInterval(sendPing, 30 * 1000, ws);
|
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
hterm.defaultStorage = new lib.Storage.Local();
|
|
|
|
hterm.defaultStorage.clear();
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
term = new hterm.Terminal();
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-25 02:23:38 +00:00
|
|
|
term.getPrefs().set("send-encoding", "raw");
|
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
term.onTerminalReady = function() {
|
|
|
|
var io = term.io.push();
|
|
|
|
|
|
|
|
io.onVTKeystroke = function(str) {
|
|
|
|
ws.send("0" + str);
|
|
|
|
};
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
io.sendString = io.onVTKeystroke;
|
2015-08-16 09:47:23 +00:00
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
io.onTerminalResize = function(columns, rows) {
|
|
|
|
ws.send(
|
2015-09-01 04:27:43 +00:00
|
|
|
"2" + JSON.stringify(
|
2015-08-23 22:14:24 +00:00
|
|
|
{
|
|
|
|
columns: columns,
|
|
|
|
rows: rows,
|
|
|
|
}
|
|
|
|
)
|
2015-08-16 09:47:23 +00:00
|
|
|
)
|
2015-08-23 22:14:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
term.installKeyboard();
|
2015-08-16 09:47:23 +00:00
|
|
|
};
|
|
|
|
|
2015-08-29 04:12:04 +00:00
|
|
|
term.decorate(document.getElementById("terminal"));
|
2015-08-16 09:47:23 +00:00
|
|
|
};
|
|
|
|
|
2015-08-23 22:14:24 +00:00
|
|
|
ws.onmessage = function(event) {
|
|
|
|
data = event.data.slice(1);
|
|
|
|
switch(event.data[0]) {
|
|
|
|
case '0':
|
2015-09-03 03:16:35 +00:00
|
|
|
term.io.writeUTF8(window.atob(data));
|
2015-08-23 22:14:24 +00:00
|
|
|
break;
|
|
|
|
case '1':
|
2015-09-01 04:27:43 +00:00
|
|
|
// pong
|
2015-10-06 19:34:40 +00:00
|
|
|
break;
|
2015-09-01 04:27:43 +00:00
|
|
|
case '2':
|
2015-08-23 22:14:24 +00:00
|
|
|
term.setWindowTitle(data);
|
|
|
|
break;
|
2015-09-01 04:27:43 +00:00
|
|
|
case '3':
|
2015-08-23 22:14:24 +00:00
|
|
|
preferences = JSON.parse(data);
|
|
|
|
Object.keys(preferences).forEach(function(key) {
|
2015-08-29 21:49:34 +00:00
|
|
|
console.log("Setting " + key + ": " + preferences[key]);
|
2015-08-23 22:14:24 +00:00
|
|
|
term.getPrefs().set(key, preferences[key]);
|
|
|
|
});
|
|
|
|
break;
|
2015-09-01 04:27:43 +00:00
|
|
|
case '4':
|
2015-08-23 22:14:24 +00:00
|
|
|
autoReconnect = JSON.parse(data);
|
2015-09-01 04:27:43 +00:00
|
|
|
console.log("Enabling reconnect: " + autoReconnect + " seconds")
|
2015-08-23 22:14:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-01 04:27:43 +00:00
|
|
|
};
|
2015-08-23 22:14:24 +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-08-23 22:14:24 +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
|
|
|
})()
|