diff --git a/package.json b/package.json index 1c3b5d55..23b4c949 100644 --- a/package.json +++ b/package.json @@ -263,6 +263,7 @@ "conf": "13.1.0", "custom-electron-prompt": "1.5.8", "deepmerge-ts": "7.1.5", + "delay": "6.0.0", "electron-debug": "4.1.0", "electron-is": "3.0.0", "electron-localshortcut": "3.2.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cf25ad62..b24b58e2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -105,6 +105,9 @@ importers: deepmerge-ts: specifier: 7.1.5 version: 7.1.5 + delay: + specifier: 6.0.0 + version: 6.0.0 electron-debug: specifier: 4.1.0 version: 4.1.0 @@ -2026,6 +2029,10 @@ packages: resolution: {integrity: sha512-R6ep6JJ+eOBZsBr9esiNN1gxFbZE4Q2cULkUSFumGYecAiS6qodDvcPx/sFuWHMNul7DWmrtoEOpYSm7o6tbSA==} engines: {node: '>=18'} + delay@6.0.0: + resolution: {integrity: sha512-2NJozoOHQ4NuZuVIr5CWd0iiLVIRSDepakaovIN+9eIDHEhdCAEvSy2cuf1DCrPPQLvHmbqTHODlhHg8UCy4zw==} + engines: {node: '>=16'} + delayed-stream@1.0.0: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} @@ -6546,6 +6553,8 @@ snapshots: p-map: 7.0.3 slash: 5.1.0 + delay@6.0.0: {} + delayed-stream@1.0.0: {} detect-libc@2.0.3: {} diff --git a/src/plugins/music-together/connection.ts b/src/plugins/music-together/connection.ts index 3c71dcb8..5d211fe5 100644 --- a/src/plugins/music-together/connection.ts +++ b/src/plugins/music-together/connection.ts @@ -1,4 +1,5 @@ -import { DataConnection, Peer } from 'peerjs'; +import { DataConnection, Peer, PeerErrorType } from 'peerjs'; +import delay from 'delay'; import type { Permission, Profile, VideoData } from './types'; @@ -54,16 +55,16 @@ export class Connection { this._mode = 'host'; this.waitOpen.resolve(id); }); - this.peer.on('connection', (conn) => { + this.peer.on('connection', async (conn) => { this._mode = 'host'; - this.registerConnection(conn); + await this.registerConnection(conn); }); this.peer.on('error', (err) => { this._mode = 'disconnected'; this.waitOpen.reject(err); this.connectionListeners.forEach((listener) => listener()); - console.log(err); + console.error(err); }); } @@ -74,7 +75,9 @@ export class Connection { async connect(id: string) { this._mode = 'guest'; - const conn = this.peer.connect(id); + const conn = this.peer.connect(id, { + reliable: true, + }); await this.registerConnection(conn); return conn; } @@ -120,7 +123,17 @@ export class Connection { /* privates */ private async registerConnection(conn: DataConnection) { return new Promise((resolve, reject) => { - this.peer.once('error', (err) => { + this.peer.once('error', async (err) => { + if (err.type === PeerErrorType.Network) { + // retrying after 10 seconds + await delay(10000); + try { + this.peer.reconnect(); + return; + } catch { + //ignored + } + } this._mode = 'disconnected'; reject(err);