mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-13 03:11:46 +00:00
fix(music-together): set connection as reliable, add retry logic
This commit is contained in:
@ -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<DataConnection>((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);
|
||||
|
||||
Reference in New Issue
Block a user