Skip to content

Commit f341156

Browse files
committed
use custom reconnect logic for socketio instead of what is built in, due to socketio/socket.io#5197
1 parent 8e98004 commit f341156

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/packages/conat/core/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ export class Client extends EventEmitter {
393393
// all client connections created using this Client.
394394
clients: { [subject: string]: { [id: string]: ConatSocketClient } };
395395
} = { servers: {}, clients: {} };
396-
private readonly options: ClientOptions;
396+
public readonly options: ClientOptions;
397397
private inboxSubject: string;
398398
private inbox?: EventEmitter;
399399
private permissionError = {

src/packages/frontend/conat/client.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { info as refCacheInfo } from "@cocalc/util/refcache";
4040
import { connect as connectToConat } from "@cocalc/conat/core/client";
4141
import type { ConnectionStats } from "@cocalc/conat/core/types";
4242
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
43-
import { once } from "@cocalc/util/async-utils";
43+
import { once, until } from "@cocalc/util/async-utils";
4444
import { delay } from "awaiting";
4545
import {
4646
deleteRememberMe,
@@ -95,6 +95,10 @@ export class ConatClient extends EventEmitter {
9595
this._conatClient = connectToConat({
9696
address,
9797
inboxPrefix: inboxPrefix({ account_id: this.client.account_id }),
98+
// it is necessary to manually managed reconnects due to a bugs
99+
// in socketio that has stumped their devs
100+
// -- https://github.com/socketio/socket.io/issues/5197
101+
reconnection: false,
98102
});
99103
this._conatClient.on("connected", () => {
100104
this.setConnectionStatus({
@@ -113,6 +117,7 @@ export class ConatClient extends EventEmitter {
113117
stats: this._conatClient?.stats,
114118
});
115119
this.client.emit("disconnected", "offline");
120+
setTimeout(this.connect, 1000);
116121
});
117122
this._conatClient.conn.io.on("reconnect_attempt", (attempt) => {
118123
this.numConnectionAttempts = attempt;
@@ -226,15 +231,38 @@ export class ConatClient extends EventEmitter {
226231

227232
// if there is a connection, resume it
228233
resume = () => {
229-
if (this.permanentlyDisconnected) {
230-
console.log(
231-
"Not connecting -- client is permanently disconnected and must refresh their browser",
232-
);
233-
return;
234-
}
235-
this._conatClient?.conn.io.connect();
234+
this.connect();
236235
};
237236

237+
// keep trying until connected.
238+
connect = reuseInFlight(async () => {
239+
let attempts = 0;
240+
await until(
241+
async () => {
242+
if (this.permanentlyDisconnected) {
243+
console.log(
244+
"Not connecting -- client is permanently disconnected and must refresh their browser",
245+
);
246+
return true;
247+
}
248+
if (this._conatClient == null) {
249+
this.conat();
250+
}
251+
if (this._conatClient?.conn?.connected) {
252+
return true;
253+
}
254+
await waitForOnline();
255+
attempts += 1;
256+
console.log(
257+
`Connecting to ${this._conatClient?.options.address}: attempts ${attempts}`,
258+
);
259+
this._conatClient?.conn.io.connect();
260+
return false;
261+
},
262+
{ min: 3000, max: 15000 },
263+
);
264+
});
265+
238266
callConatService: CallConatServiceFunction = async (options) => {
239267
return await callConatService(options);
240268
};
@@ -498,3 +526,14 @@ function setNotDeleted({ project_id, path }) {
498526
const actions = redux.getProjectActions(project_id);
499527
actions?.setRecentlyDeleted(path, 0);
500528
}
529+
530+
async function waitForOnline(): Promise<void> {
531+
if (navigator.onLine) return;
532+
await new Promise<void>((resolve) => {
533+
const handler = () => {
534+
window.removeEventListener("online", handler);
535+
resolve();
536+
};
537+
window.addEventListener("online", handler);
538+
});
539+
}

0 commit comments

Comments
 (0)