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