Skip to content

Commit

Permalink
html: drop backoff support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed May 4, 2021
1 parent 99cfaa9 commit d7440fb
Show file tree
Hide file tree
Showing 5 changed files with 8,762 additions and 9,800 deletions.
1 change: 0 additions & 1 deletion html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"webpack-merge": "^5.7.3"
},
"dependencies": {
"backoff": "^2.5.0",
"decko": "^1.2.0",
"file-saver": "^2.0.5",
"preact": "^10.5.13",
Expand Down
50 changes: 14 additions & 36 deletions html/src/components/terminal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bind } from 'decko';
import * as backoff from 'backoff';
import { Component, h } from 'preact';
import { ITerminalOptions, Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
Expand Down Expand Up @@ -61,15 +60,13 @@ export class Xterm extends Component<Props> {

private socket: WebSocket;
private token: string;
private opened = false;
private title: string;
private titleFixed: string;
private resizeTimeout: number;
private resizeOverlay = true;

private backoff: backoff.Backoff;
private backoffLock = false;
private doBackoff = true;
private reconnect = false;
private reconnect = true;
private doReconnect = true;

constructor(props: Props) {
super(props);
Expand All @@ -78,23 +75,6 @@ export class Xterm extends Component<Props> {
this.textDecoder = new TextDecoder();
this.fitAddon = new FitAddon();
this.overlayAddon = new OverlayAddon();
this.backoff = backoff.exponential({
initialDelay: 100,
maxDelay: 10000,
});
this.backoff.failAfter(15);
this.backoff.on('ready', () => {
this.backoffLock = false;
this.overlayAddon.showOverlay('Reconnecting...', null);
this.refreshToken().then(this.connect);
});
this.backoff.on('backoff', (_, delay: number) => {
console.log(`[ttyd] will attempt to reconnect websocket in ${delay}ms`);
this.backoffLock = true;
});
this.backoff.on('fail', () => {
this.backoffLock = true; // break backoff
});
}

async componentDidMount() {
Expand Down Expand Up @@ -261,7 +241,7 @@ export class Xterm extends Component<Props> {
case 'disableReconnect':
if (value) {
console.log(`[ttyd] Reconnect disabled`);
this.doBackoff = false;
this.reconnect = false;
}
break;
case 'titleFixed':
Expand All @@ -282,7 +262,6 @@ export class Xterm extends Component<Props> {
@bind
private onSocketOpen() {
console.log('[ttyd] websocket connection opened');
this.backoff.reset();

const { socket, textEncoder, terminal, fitAddon, overlayAddon } = this;
const dims = fitAddon.proposeDimensions();
Expand All @@ -296,16 +275,17 @@ export class Xterm extends Component<Props> {
)
);

if (this.reconnect) {
if (this.opened) {
terminal.reset();
terminal.resize(dims.cols, dims.rows);
overlayAddon.showOverlay('Reconnected', 300);
} else {
this.reconnect = true;
this.opened = true;
fitAddon.fit();
}

this.applyOptions(this.props.clientOptions);
this.doReconnect = this.reconnect;

terminal.focus();
}
Expand All @@ -314,14 +294,15 @@ export class Xterm extends Component<Props> {
private onSocketClose(event: CloseEvent) {
console.log(`[ttyd] websocket connection closed with code: ${event.code}`);

const { backoff, doBackoff, backoffLock, overlayAddon } = this;
const { refreshToken, connect, doReconnect, overlayAddon } = this;
overlayAddon.showOverlay('Connection Closed', null);

// 1000: CLOSE_NORMAL
if (event.code !== 1000 && doBackoff && !backoffLock) {
backoff.backoff();
} else if (!doBackoff) {
const { terminal, refreshToken, connect } = this;
if (event.code !== 1000 && doReconnect) {
overlayAddon.showOverlay('Reconnecting...', null);
refreshToken().then(connect);
} else {
const { terminal } = this;
const keyDispose = terminal.onKey(e => {
const event = e.domEvent;
if (event.key === 'Enter') {
Expand All @@ -337,10 +318,7 @@ export class Xterm extends Component<Props> {
@bind
private onSocketError(event: Event) {
console.error('[ttyd] websocket connection error: ', event);
const { backoff, doBackoff, backoffLock } = this;
if (doBackoff && !backoffLock) {
backoff.backoff();
}
this.doReconnect = false;
}

@bind
Expand Down
7 changes: 1 addition & 6 deletions html/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const path = require('path');
const webpack = require('webpack');
const { merge } = require('webpack-merge');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
Expand Down Expand Up @@ -41,13 +40,9 @@ const baseConfig = {
]
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ],
fallback: { "util": require.resolve("util/") }
extensions: [ '.tsx', '.ts', '.js' ]
},
plugins: [
new webpack.DefinePlugin({
"process.env.NODE_DEBUG": JSON.stringify(process.env.NODE_DEBUG), // used by util
}),
new CopyWebpackPlugin({
patterns:[
{ from: './favicon.png', to: '.' }
Expand Down
12 changes: 0 additions & 12 deletions html/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -711,13 +711,6 @@ bach@^1.0.0:
async-settle "^1.0.0"
now-and-later "^2.0.0"

backoff@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/backoff/-/backoff-2.5.0.tgz#f616eda9d3e4b66b8ca7fca79f695722c5f8e26f"
integrity sha1-9hbtqdPktmuMp/ynn2lXIsX44m8=
dependencies:
precond "0.2"

balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
Expand Down Expand Up @@ -5547,11 +5540,6 @@ preact@^10.5.13:
resolved "https://registry.yarnpkg.com/preact/-/preact-10.5.13.tgz#85f6c9197ecd736ce8e3bec044d08fd1330fa019"
integrity sha512-q/vlKIGNwzTLu+jCcvywgGrt+H/1P/oIRSD6mV4ln3hmlC+Aa34C7yfPI4+5bzW8pONyVXYS7SvXosy2dKKtWQ==

precond@0.2:
version "0.2.3"
resolved "https://registry.yarnpkg.com/precond/-/precond-0.2.3.tgz#aa9591bcaa24923f1e0f4849d240f47efc1075ac"
integrity sha1-qpWRvKokkj8eD0hJ0kD0fvwQdaw=

prepend-http@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897"
Expand Down
Loading

0 comments on commit d7440fb

Please sign in to comment.