mirror of
https://github.com/yudai/gotty.git
synced 2025-09-21 21:40:49 +02:00
Support auto reconnection
A new option `--auto-reconnect` which takes seconds to reconnect is added.
This commit is contained in:
parent
4df9ac8059
commit
acacba6f03
6 changed files with 97 additions and 55 deletions
|
@ -2,62 +2,84 @@
|
|||
var httpsEnabled = window.location.protocol == "https:";
|
||||
var url = (httpsEnabled ? 'wss://' : 'ws://') + window.location.host + window.location.pathname + 'ws';
|
||||
var protocols = ["gotty"];
|
||||
var ws = new WebSocket(url, protocols);
|
||||
var autoReconnect = -1;
|
||||
|
||||
var term;
|
||||
var openWs = function() {
|
||||
var ws = new WebSocket(url, protocols);
|
||||
|
||||
ws.onopen = function(event) {
|
||||
hterm.defaultStorage = new lib.Storage.Local();
|
||||
hterm.defaultStorage.clear();
|
||||
var term;
|
||||
|
||||
term = new hterm.Terminal();
|
||||
ws.onopen = function(event) {
|
||||
hterm.defaultStorage = new lib.Storage.Local();
|
||||
hterm.defaultStorage.clear();
|
||||
|
||||
term.onTerminalReady = function() {
|
||||
var io = term.io.push();
|
||||
term = new hterm.Terminal();
|
||||
|
||||
io.onVTKeystroke = function(str) {
|
||||
ws.send("0" + str);
|
||||
};
|
||||
term.onTerminalReady = function() {
|
||||
var io = term.io.push();
|
||||
|
||||
io.sendString = io.onVTKeystroke;
|
||||
io.onVTKeystroke = function(str) {
|
||||
ws.send("0" + str);
|
||||
};
|
||||
|
||||
io.onTerminalResize = function(columns, rows) {
|
||||
ws.send(
|
||||
"1" + JSON.stringify(
|
||||
{
|
||||
columns: columns,
|
||||
rows: rows,
|
||||
}
|
||||
io.sendString = io.onVTKeystroke;
|
||||
|
||||
io.onTerminalResize = function(columns, rows) {
|
||||
ws.send(
|
||||
"1" + JSON.stringify(
|
||||
{
|
||||
columns: columns,
|
||||
rows: rows,
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
};
|
||||
|
||||
term.installKeyboard();
|
||||
};
|
||||
|
||||
term.installKeyboard();
|
||||
term.decorate(document.body);
|
||||
};
|
||||
|
||||
term.decorate(document.body);
|
||||
};
|
||||
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) {
|
||||
term.getPrefs().set(key, preferences[key]);
|
||||
});
|
||||
break;
|
||||
case '3':
|
||||
autoReconnect = JSON.parse(data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
term.getPrefs().set(key, preferences[key]);
|
||||
});
|
||||
break;
|
||||
ws.onclose = function(event) {
|
||||
if (term) {
|
||||
term.uninstallKeyboard();
|
||||
term.io.showOverlay("Connection Closed", null);
|
||||
}
|
||||
tryReconnect();
|
||||
}
|
||||
|
||||
ws.onerror = function(error) {
|
||||
tryReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
ws.onclose = function(event) {
|
||||
term.io.showOverlay("Connection Closed", null);
|
||||
term.uninstallKeyboard();
|
||||
openWs();
|
||||
|
||||
var tryReconnect = function() {
|
||||
if (autoReconnect >= 0) {
|
||||
setTimeout(openWs, autoReconnect * 1000);
|
||||
}
|
||||
}
|
||||
})()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue