Add disconnect/reconnect commands

This commit is contained in:
Simon Ser 2021-01-12 10:35:38 +01:00
parent 4b165054a4
commit f35f316577
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
2 changed files with 44 additions and 15 deletions

View file

@ -88,4 +88,10 @@ export default {
} }
app.client.send({ command: "TOPIC", params }); app.client.send({ command: "TOPIC", params });
}, },
"reconnect": (app, args) => {
app.reconnect();
},
"disconnect": (app, args) => {
app.disconnect();
},
}; };

View file

@ -107,10 +107,12 @@ export default class App extends Component {
receipts = new Map(); receipts = new Map();
buffer = createRef(); buffer = createRef();
composer = createRef(); composer = createRef();
reconnectTimeoutID = null;
constructor(props) { constructor(props) {
super(props); super(props);
this.handleClose = this.handleClose.bind(this);
this.handleConnectSubmit = this.handleConnectSubmit.bind(this); this.handleConnectSubmit = this.handleConnectSubmit.bind(this);
this.handleBufferListClick = this.handleBufferListClick.bind(this); this.handleBufferListClick = this.handleBufferListClick.bind(this);
this.handleComposerSubmit = this.handleComposerSubmit.bind(this); this.handleComposerSubmit = this.handleComposerSubmit.bind(this);
@ -347,6 +349,8 @@ export default class App extends Component {
} }
connect(params) { connect(params) {
this.disconnect();
this.setState({ status: Status.CONNECTING, connectParams: params }); this.setState({ status: Status.CONNECTING, connectParams: params });
this.client = new Client({ this.client = new Client({
@ -358,19 +362,7 @@ export default class App extends Component {
saslPlain: params.saslPlain, saslPlain: params.saslPlain,
}); });
this.client.addEventListener("close", () => { this.client.addEventListener("close", this.handleClose);
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 };
});
});
this.client.addEventListener("message", (event) => { this.client.addEventListener("message", (event) => {
this.handleMessage(event.detail.message); this.handleMessage(event.detail.message);
@ -386,6 +378,38 @@ export default class App extends Component {
this.switchBuffer(SERVER_BUFFER); this.switchBuffer(SERVER_BUFFER);
} }
handleClose() {
this.setState((state) => {
if (state.status == 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(this.state.connectParams);
}, RECONNECT_DELAY_SEC * 1000);
return { status: Status.DISCONNECTED };
});
}
disconnect() {
clearTimeout(this.reconnectTimeoutID);
this.reconnectTimeoutID = null;
if (this.client) {
// Prevent auto-reconnect from kicking in
this.client.removeEventListener("close", this.handleClose);
this.client.close();
}
this.setState({ status: Status.DISCONNECTED });
}
reconnect() {
this.connect(this.state.connectParams);
}
handleMessage(msg) { handleMessage(msg) {
switch (msg.command) { switch (msg.command) {
case irc.RPL_WELCOME: case irc.RPL_WELCOME:
@ -614,11 +638,10 @@ export default class App extends Component {
close(target) { close(target) {
if (target == SERVER_BUFFER) { if (target == SERVER_BUFFER) {
this.setState({ this.setState({
status: Status.DISCONNECTED,
buffers: new Map(), buffers: new Map(),
activeBuffer: null, activeBuffer: null,
}); });
this.client.close(); this.disconnect();
return; return;
} }