mirror of
https://codeberg.org/emersion/gamja.git
synced 2024-11-14 10:55:06 -05:00
Compare commits
5 commits
ad165389f0
...
afa09cfc25
Author | SHA1 | Date | |
---|---|---|---|
|
afa09cfc25 | ||
|
977752e0f2 | ||
|
4bce52f162 | ||
|
75ec7cd212 | ||
|
24e6767cab |
2 changed files with 25 additions and 10 deletions
|
@ -1,4 +1,5 @@
|
||||||
image: alpine/latest
|
# TODO switch back to alpine/latest once the "npm install" deadlock is fixed
|
||||||
|
image: alpine/edge
|
||||||
packages:
|
packages:
|
||||||
- npm
|
- npm
|
||||||
- rsync
|
- rsync
|
||||||
|
|
|
@ -74,8 +74,8 @@ class IRCError extends Error {
|
||||||
class Backoff {
|
class Backoff {
|
||||||
n = 0;
|
n = 0;
|
||||||
|
|
||||||
constructor(min, max) {
|
constructor(base, max) {
|
||||||
this.min = min;
|
this.base = base;
|
||||||
this.max = max;
|
this.max = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,10 +86,10 @@ class Backoff {
|
||||||
next() {
|
next() {
|
||||||
if (this.n === 0) {
|
if (this.n === 0) {
|
||||||
this.n = 1;
|
this.n = 1;
|
||||||
return this.min;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let dur = this.n * this.min;
|
let dur = this.n * this.base;
|
||||||
if (dur > this.max) {
|
if (dur > this.max) {
|
||||||
dur = this.max;
|
dur = this.max;
|
||||||
} else {
|
} else {
|
||||||
|
@ -134,6 +134,7 @@ export default class Client extends EventTarget {
|
||||||
autoReconnect = true;
|
autoReconnect = true;
|
||||||
reconnectTimeoutID = null;
|
reconnectTimeoutID = null;
|
||||||
reconnectBackoff = new Backoff(RECONNECT_MIN_DELAY_MSEC, RECONNECT_MAX_DELAY_MSEC);
|
reconnectBackoff = new Backoff(RECONNECT_MIN_DELAY_MSEC, RECONNECT_MAX_DELAY_MSEC);
|
||||||
|
lastReconnectDate = new Date(0);
|
||||||
pingIntervalID = null;
|
pingIntervalID = null;
|
||||||
pendingCmds = {
|
pendingCmds = {
|
||||||
WHO: Promise.resolve(null),
|
WHO: Promise.resolve(null),
|
||||||
|
@ -147,6 +148,8 @@ export default class Client extends EventTarget {
|
||||||
constructor(params) {
|
constructor(params) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
this.handleOnline = this.handleOnline.bind(this);
|
||||||
|
|
||||||
this.params = { ...this.params, ...params };
|
this.params = { ...this.params, ...params };
|
||||||
|
|
||||||
this.reconnect();
|
this.reconnect();
|
||||||
|
@ -159,6 +162,7 @@ export default class Client extends EventTarget {
|
||||||
|
|
||||||
console.log("Connecting to " + this.params.url);
|
console.log("Connecting to " + this.params.url);
|
||||||
this.setStatus(Client.Status.CONNECTING);
|
this.setStatus(Client.Status.CONNECTING);
|
||||||
|
this.lastReconnectDate = new Date();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.ws = new WebSocket(this.params.url);
|
this.ws = new WebSocket(this.params.url);
|
||||||
|
@ -201,15 +205,16 @@ export default class Client extends EventTarget {
|
||||||
this.monitored = new irc.CaseMapMap(null, irc.CaseMapping.RFC1459);
|
this.monitored = new irc.CaseMapMap(null, irc.CaseMapping.RFC1459);
|
||||||
|
|
||||||
if (this.autoReconnect) {
|
if (this.autoReconnect) {
|
||||||
|
window.addEventListener("online", this.handleOnline);
|
||||||
|
|
||||||
if (!navigator.onLine) {
|
if (!navigator.onLine) {
|
||||||
console.info("Waiting for network to go back online");
|
console.info("Waiting for network to go back online");
|
||||||
const handleOnline = () => {
|
|
||||||
window.removeEventListener("online", handleOnline);
|
|
||||||
this.reconnect();
|
|
||||||
};
|
|
||||||
window.addEventListener("online", handleOnline);
|
|
||||||
} else {
|
} else {
|
||||||
let delay = this.reconnectBackoff.next();
|
let delay = this.reconnectBackoff.next();
|
||||||
|
let sinceLastReconnect = new Date().getTime() - this.lastReconnectDate.getTime();
|
||||||
|
if (sinceLastReconnect < RECONNECT_MIN_DELAY_MSEC) {
|
||||||
|
delay = Math.max(delay, RECONNECT_MIN_DELAY_MSEC);
|
||||||
|
}
|
||||||
console.info("Reconnecting to server in " + (delay / 1000) + " seconds");
|
console.info("Reconnecting to server in " + (delay / 1000) + " seconds");
|
||||||
clearTimeout(this.reconnectTimeoutID);
|
clearTimeout(this.reconnectTimeoutID);
|
||||||
this.reconnectTimeoutID = setTimeout(() => {
|
this.reconnectTimeoutID = setTimeout(() => {
|
||||||
|
@ -226,6 +231,8 @@ export default class Client extends EventTarget {
|
||||||
clearTimeout(this.reconnectTimeoutID);
|
clearTimeout(this.reconnectTimeoutID);
|
||||||
this.reconnectTimeoutID = null;
|
this.reconnectTimeoutID = null;
|
||||||
|
|
||||||
|
window.removeEventListener("online", this.handleOnline);
|
||||||
|
|
||||||
this.setPingInterval(0);
|
this.setPingInterval(0);
|
||||||
|
|
||||||
if (this.ws) {
|
if (this.ws) {
|
||||||
|
@ -245,6 +252,13 @@ export default class Client extends EventTarget {
|
||||||
this.dispatchEvent(new CustomEvent("error", { detail: err }));
|
this.dispatchEvent(new CustomEvent("error", { detail: err }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleOnline() {
|
||||||
|
window.removeEventListener("online", this.handleOnline);
|
||||||
|
if (this.autoReconnect && this.status === Client.Status.DISCONNECTED) {
|
||||||
|
this.reconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
handleOpen() {
|
handleOpen() {
|
||||||
console.log("Connection opened");
|
console.log("Connection opened");
|
||||||
this.setStatus(Client.Status.REGISTERING);
|
this.setStatus(Client.Status.REGISTERING);
|
||||||
|
|
Loading…
Reference in a new issue