From 4b165054a459707179c590ac6b5b6781c7662012 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 11 Jan 2021 18:12:28 +0100 Subject: [PATCH] Reconnect when loosing connection --- commands.js | 2 +- components/app.js | 22 +++++++++++++++++----- components/buffer-header.js | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/commands.js b/commands.js index 7cd9a58..72bd036 100644 --- a/commands.js +++ b/commands.js @@ -12,7 +12,7 @@ export default { if (window.localStorage) { localStorage.removeItem("autoconnect"); } - app.client.close(); + app.close(SERVER_BUFFER); }, "query": (app, args) => { var nick = args[0]; diff --git a/components/app.js b/components/app.js index a10baff..f57bdfd 100644 --- a/components/app.js +++ b/components/app.js @@ -15,6 +15,7 @@ import { setup as setupKeybindings } from "/keybindings.js"; const CHATHISTORY_PAGE_SIZE = 100; const CHATHISTORY_MAX_SIZE = 4000; +const RECONNECT_DELAY_SEC = 10; var messagesCount = 0; @@ -358,10 +359,16 @@ export default class App extends Component { }); this.client.addEventListener("close", () => { - this.setState({ - status: Status.DISCONNECTED, - buffers: new Map(), - activeBuffer: null, + this.setState((state) => { + if (state.status == Status.DISCONNECTED) { + // User decided to logout + return null; + } + console.log("Reconnecting to server in " + RECONNECT_DELAY_SEC + " seconds"); + setTimeout(() => { + this.connect(params); + }, RECONNECT_DELAY_SEC * 1000); + return { status: Status.DISCONNECTED }; }); }); @@ -606,6 +613,11 @@ export default class App extends Component { close(target) { if (target == SERVER_BUFFER) { + this.setState({ + status: Status.DISCONNECTED, + buffers: new Map(), + activeBuffer: null, + }); this.client.close(); return; } @@ -811,7 +823,7 @@ export default class App extends Component { } render() { - if (this.state.status != Status.REGISTERED) { + if (this.state.status != Status.REGISTERED && !this.state.activeBuffer) { return html`
<${Connect} error=${this.state.error} params=${this.state.connectParams} disabled=${this.state.status != Status.DISCONNECTED} onSubmit=${this.handleConnectSubmit}/> diff --git a/components/buffer-header.js b/components/buffer-header.js index 7635c48..dbcc517 100644 --- a/components/buffer-header.js +++ b/components/buffer-header.js @@ -27,6 +27,7 @@ export default function BufferHeader(props) { var description = null; if (props.buffer.serverInfo) { + // TODO: print current connection status var serverInfo = props.buffer.serverInfo; description = `Connected to ${serverInfo.name}`; } else if (props.buffer.topic) {