Reconnect when loosing connection

This commit is contained in:
Simon Ser 2021-01-11 18:12:28 +01:00
parent 57ca2c44ad
commit 4b165054a4
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 19 additions and 6 deletions

View file

@ -12,7 +12,7 @@ export default {
if (window.localStorage) { if (window.localStorage) {
localStorage.removeItem("autoconnect"); localStorage.removeItem("autoconnect");
} }
app.client.close(); app.close(SERVER_BUFFER);
}, },
"query": (app, args) => { "query": (app, args) => {
var nick = args[0]; var nick = args[0];

View file

@ -15,6 +15,7 @@ import { setup as setupKeybindings } from "/keybindings.js";
const CHATHISTORY_PAGE_SIZE = 100; const CHATHISTORY_PAGE_SIZE = 100;
const CHATHISTORY_MAX_SIZE = 4000; const CHATHISTORY_MAX_SIZE = 4000;
const RECONNECT_DELAY_SEC = 10;
var messagesCount = 0; var messagesCount = 0;
@ -358,10 +359,16 @@ export default class App extends Component {
}); });
this.client.addEventListener("close", () => { this.client.addEventListener("close", () => {
this.setState({ this.setState((state) => {
status: Status.DISCONNECTED, if (state.status == Status.DISCONNECTED) {
buffers: new Map(), // User decided to logout
activeBuffer: null, 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) { close(target) {
if (target == SERVER_BUFFER) { if (target == SERVER_BUFFER) {
this.setState({
status: Status.DISCONNECTED,
buffers: new Map(),
activeBuffer: null,
});
this.client.close(); this.client.close();
return; return;
} }
@ -811,7 +823,7 @@ export default class App extends Component {
} }
render() { render() {
if (this.state.status != Status.REGISTERED) { if (this.state.status != Status.REGISTERED && !this.state.activeBuffer) {
return html` return html`
<section id="connect"> <section id="connect">
<${Connect} error=${this.state.error} params=${this.state.connectParams} disabled=${this.state.status != Status.DISCONNECTED} onSubmit=${this.handleConnectSubmit}/> <${Connect} error=${this.state.error} params=${this.state.connectParams} disabled=${this.state.status != Status.DISCONNECTED} onSubmit=${this.handleConnectSubmit}/>

View file

@ -27,6 +27,7 @@ export default function BufferHeader(props) {
var description = null; var description = null;
if (props.buffer.serverInfo) { if (props.buffer.serverInfo) {
// TODO: print current connection status
var serverInfo = props.buffer.serverInfo; var serverInfo = props.buffer.serverInfo;
description = `Connected to ${serverInfo.name}`; description = `Connected to ${serverInfo.name}`;
} else if (props.buffer.topic) { } else if (props.buffer.topic) {