mirror of
https://codeberg.org/emersion/gamja
synced 2025-03-13 07:48:37 +01:00
Move auto-reconnect logic into Client
This commit is contained in:
parent
41cd2153cf
commit
4e1f06b960
@ -15,7 +15,6 @@ 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;
|
|
||||||
|
|
||||||
const DEFAULT_NETWORK = "network"; // TODO: remove this global
|
const DEFAULT_NETWORK = "network"; // TODO: remove this global
|
||||||
|
|
||||||
@ -161,7 +160,6 @@ export default class App extends Component {
|
|||||||
receipts = new Map();
|
receipts = new Map();
|
||||||
buffer = createRef();
|
buffer = createRef();
|
||||||
composer = createRef();
|
composer = createRef();
|
||||||
reconnectTimeoutID = null;
|
|
||||||
lastNetworkID = 0;
|
lastNetworkID = 0;
|
||||||
lastBufferID = 0;
|
lastBufferID = 0;
|
||||||
|
|
||||||
@ -438,13 +436,9 @@ export default class App extends Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
connect(netID, params) {
|
connect(params) {
|
||||||
if (netID) {
|
|
||||||
this.disconnect(netID);
|
|
||||||
} else {
|
|
||||||
this.lastNetworkID++;
|
this.lastNetworkID++;
|
||||||
netID = this.lastNetworkID;
|
var netID = this.lastNetworkID;
|
||||||
}
|
|
||||||
|
|
||||||
this.setState((state) => {
|
this.setState((state) => {
|
||||||
var networks = new Map(state.networks);
|
var networks = new Map(state.networks);
|
||||||
@ -465,11 +459,10 @@ export default class App extends Component {
|
|||||||
realname: params.realname,
|
realname: params.realname,
|
||||||
saslPlain: params.saslPlain,
|
saslPlain: params.saslPlain,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.clients.set(netID, client);
|
this.clients.set(netID, client);
|
||||||
|
|
||||||
client.addEventListener("status", () => {
|
client.addEventListener("status", () => {
|
||||||
this.handleStatus(netID, client.status);
|
this.setNetworkState(netID, { status: client.status });
|
||||||
});
|
});
|
||||||
|
|
||||||
client.addEventListener("message", (event) => {
|
client.addEventListener("message", (event) => {
|
||||||
@ -486,55 +479,26 @@ export default class App extends Component {
|
|||||||
this.switchBuffer({ network: netID, name: SERVER_BUFFER });
|
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) {
|
disconnect(netID) {
|
||||||
if (!netID) {
|
if (!netID) {
|
||||||
netID = getActiveNetworkID(this.state);
|
netID = getActiveNetworkID(this.state);
|
||||||
if (!netID) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
clearTimeout(this.reconnectTimeoutID);
|
|
||||||
this.reconnectTimeoutID = null;
|
|
||||||
|
|
||||||
var client = this.clients.get(netID);
|
var client = this.clients.get(netID);
|
||||||
if (client) {
|
if (client) {
|
||||||
client.disconnect();
|
client.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setNetworkState(netID, { status: NetworkStatus.DISCONNECTED });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reconnect(netID) {
|
reconnect(netID) {
|
||||||
if (!netID) {
|
if (!netID) {
|
||||||
netID = getActiveNetworkID(this.state);
|
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) {
|
handleMessage(netID, msg) {
|
||||||
@ -747,7 +711,7 @@ export default class App extends Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.connect(null, connectParams);
|
this.connect(connectParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleNickClick(nick) {
|
handleNickClick(nick) {
|
||||||
@ -997,7 +961,7 @@ export default class App extends Component {
|
|||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
if (this.state.connectParams.autoconnect) {
|
if (this.state.connectParams.autoconnect) {
|
||||||
this.connect(null, this.state.connectParams);
|
this.connect(this.state.connectParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
setupKeybindings(this);
|
setupKeybindings(this);
|
||||||
|
@ -12,6 +12,8 @@ const permanentCaps = [
|
|||||||
"server-time",
|
"server-time",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const RECONNECT_DELAY_SEC = 10;
|
||||||
|
|
||||||
export default class Client extends EventTarget {
|
export default class Client extends EventTarget {
|
||||||
static Status = {
|
static Status = {
|
||||||
DISCONNECTED: "disconnected",
|
DISCONNECTED: "disconnected",
|
||||||
@ -21,8 +23,11 @@ export default class Client extends EventTarget {
|
|||||||
};
|
};
|
||||||
|
|
||||||
status = Client.Status.DISCONNECTED;
|
status = Client.Status.DISCONNECTED;
|
||||||
ws = null;
|
|
||||||
nick = null;
|
nick = null;
|
||||||
|
availableCaps = {};
|
||||||
|
enabledCaps = {};
|
||||||
|
|
||||||
|
ws = null;
|
||||||
params = {
|
params = {
|
||||||
url: null,
|
url: null,
|
||||||
username: null,
|
username: null,
|
||||||
@ -31,9 +36,9 @@ export default class Client extends EventTarget {
|
|||||||
pass: null,
|
pass: null,
|
||||||
saslPlain: null,
|
saslPlain: null,
|
||||||
};
|
};
|
||||||
availableCaps = {};
|
|
||||||
enabledCaps = {};
|
|
||||||
batches = new Map();
|
batches = new Map();
|
||||||
|
autoReconnect = true;
|
||||||
|
reconnectTimeoutID = null;
|
||||||
|
|
||||||
constructor(params) {
|
constructor(params) {
|
||||||
super();
|
super();
|
||||||
@ -44,7 +49,10 @@ export default class Client extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reconnect() {
|
reconnect() {
|
||||||
|
var autoReconnect = this.autoReconnect;
|
||||||
this.disconnect();
|
this.disconnect();
|
||||||
|
this.autoReconnect = autoReconnect;
|
||||||
|
|
||||||
this.setStatus(Client.Status.CONNECTING);
|
this.setStatus(Client.Status.CONNECTING);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -63,6 +71,14 @@ export default class Client extends EventTarget {
|
|||||||
console.log("Connection closed");
|
console.log("Connection closed");
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
this.setStatus(Client.Status.DISCONNECTED);
|
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", () => {
|
this.ws.addEventListener("error", () => {
|
||||||
@ -71,6 +87,11 @@ export default class Client extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
disconnect() {
|
disconnect() {
|
||||||
|
this.autoReconnect = false;
|
||||||
|
|
||||||
|
clearTimeout(this.reconnectTimeoutID);
|
||||||
|
this.reconnectTimeoutID = null;
|
||||||
|
|
||||||
if (this.ws) {
|
if (this.ws) {
|
||||||
this.ws.close(1000);
|
this.ws.close(1000);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user