From 4e1f06b9601140d629d2450efc0a637c45fee4f0 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 22 Jan 2021 18:44:06 +0100 Subject: [PATCH] Move auto-reconnect logic into Client --- components/app.js | 56 +++++++++-------------------------------------- lib/client.js | 27 ++++++++++++++++++++--- 2 files changed, 34 insertions(+), 49 deletions(-) diff --git a/components/app.js b/components/app.js index 4e57b12..a6da5d4 100644 --- a/components/app.js +++ b/components/app.js @@ -15,7 +15,6 @@ import { setup as setupKeybindings } from "/keybindings.js"; const CHATHISTORY_PAGE_SIZE = 100; const CHATHISTORY_MAX_SIZE = 4000; -const RECONNECT_DELAY_SEC = 10; const DEFAULT_NETWORK = "network"; // TODO: remove this global @@ -161,7 +160,6 @@ export default class App extends Component { receipts = new Map(); buffer = createRef(); composer = createRef(); - reconnectTimeoutID = null; lastNetworkID = 0; lastBufferID = 0; @@ -438,13 +436,9 @@ export default class App extends Component { }); } - connect(netID, params) { - if (netID) { - this.disconnect(netID); - } else { - this.lastNetworkID++; - netID = this.lastNetworkID; - } + connect(params) { + this.lastNetworkID++; + var netID = this.lastNetworkID; this.setState((state) => { var networks = new Map(state.networks); @@ -465,11 +459,10 @@ export default class App extends Component { realname: params.realname, saslPlain: params.saslPlain, }); - this.clients.set(netID, client); client.addEventListener("status", () => { - this.handleStatus(netID, client.status); + this.setNetworkState(netID, { status: client.status }); }); client.addEventListener("message", (event) => { @@ -486,55 +479,26 @@ export default class App extends Component { this.switchBuffer({ network: netID, name: SERVER_BUFFER }); } - handleStatus(netID, status) { - this.setNetworkState(netID, (state) => { - if (status !== Client.Status.DISCONNECTED) { - return { status }; - } - - if (state.status === Client.Status.DISCONNECTED) { - // User decided to logout - return null; - } - - console.log("Reconnecting to server in " + RECONNECT_DELAY_SEC + " seconds"); - clearTimeout(this.reconnectTimeoutID); - this.reconnectTimeoutID = setTimeout(() => { - this.connect(netID, this.state.connectParams); - }, RECONNECT_DELAY_SEC * 1000); - - return { status }; - }); - } - disconnect(netID) { if (!netID) { netID = getActiveNetworkID(this.state); - if (!netID) { - return; - } } - clearTimeout(this.reconnectTimeoutID); - this.reconnectTimeoutID = null; - var client = this.clients.get(netID); if (client) { client.disconnect(); } - - this.setNetworkState(netID, { status: NetworkStatus.DISCONNECTED }); } reconnect(netID) { if (!netID) { netID = getActiveNetworkID(this.state); - if (!netID) { - return; - } } - this.connect(netID, this.state.connectParams); + var client = this.clients.get(netID); + if (client) { + client.reconnect(); + } } handleMessage(netID, msg) { @@ -747,7 +711,7 @@ export default class App extends Component { } } - this.connect(null, connectParams); + this.connect(connectParams); } handleNickClick(nick) { @@ -997,7 +961,7 @@ export default class App extends Component { componentDidMount() { if (this.state.connectParams.autoconnect) { - this.connect(null, this.state.connectParams); + this.connect(this.state.connectParams); } setupKeybindings(this); diff --git a/lib/client.js b/lib/client.js index 8d8d714..37fe282 100644 --- a/lib/client.js +++ b/lib/client.js @@ -12,6 +12,8 @@ const permanentCaps = [ "server-time", ]; +const RECONNECT_DELAY_SEC = 10; + export default class Client extends EventTarget { static Status = { DISCONNECTED: "disconnected", @@ -21,8 +23,11 @@ export default class Client extends EventTarget { }; status = Client.Status.DISCONNECTED; - ws = null; nick = null; + availableCaps = {}; + enabledCaps = {}; + + ws = null; params = { url: null, username: null, @@ -31,9 +36,9 @@ export default class Client extends EventTarget { pass: null, saslPlain: null, }; - availableCaps = {}; - enabledCaps = {}; batches = new Map(); + autoReconnect = true; + reconnectTimeoutID = null; constructor(params) { super(); @@ -44,7 +49,10 @@ export default class Client extends EventTarget { } reconnect() { + var autoReconnect = this.autoReconnect; this.disconnect(); + this.autoReconnect = autoReconnect; + this.setStatus(Client.Status.CONNECTING); try { @@ -63,6 +71,14 @@ export default class Client extends EventTarget { console.log("Connection closed"); this.ws = null; this.setStatus(Client.Status.DISCONNECTED); + + if (this.autoReconnect) { + console.info("Reconnecting to server in " + RECONNECT_DELAY_SEC + " seconds"); + clearTimeout(this.reconnectTimeoutID); + this.reconnectTimeoutID = setTimeout(() => { + this.reconnect(); + }, RECONNECT_DELAY_SEC * 1000); + } }); this.ws.addEventListener("error", () => { @@ -71,6 +87,11 @@ export default class Client extends EventTarget { } disconnect() { + this.autoReconnect = false; + + clearTimeout(this.reconnectTimeoutID); + this.reconnectTimeoutID = null; + if (this.ws) { this.ws.close(1000); }