Skip to content

Commit

Permalink
fix: resumes
Browse files Browse the repository at this point in the history
  • Loading branch information
Deivu committed Aug 23, 2024
1 parent 557413b commit e13511f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Shoukaku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export type ShoukakuEvents = {
* Emitted when Shoukaku is ready to receive operations
* @eventProperty
*/
'ready': [name: string, reconnected: boolean];
'ready': [name: string, lavalinkResume: boolean, libraryResume: boolean];
/**
* Emitted when a websocket connection to Lavalink closes
* @eventProperty
Expand Down
33 changes: 16 additions & 17 deletions src/node/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ export class Node extends TypedEventEmitter<NodeEvents> {
*/
public readonly group?: string;
/**
* Websocket version this node will use
*/
public readonly version: string;
/**
* URL of Lavalink
*/
private readonly url: string;
Expand Down Expand Up @@ -160,9 +156,8 @@ export class Node extends TypedEventEmitter<NodeEvents> {
this.rest = new (this.manager.options.structures.rest ?? Rest)(this, options);
this.name = options.name;
this.group = options.group;
this.version = `/v${Versions.WEBSOCKET_VERSION}`;
this.url = `${options.secure ? 'wss' : 'ws'}://${options.url}`;
this.auth = options.auth;
this.url = `${options.secure ? 'wss' : 'ws'}://${options.url}/v${Versions.WEBSOCKET_VERSION}/websocket`;
this.reconnects = 0;
this.state = State.DISCONNECTED;
this.stats = null;
Expand Down Expand Up @@ -217,20 +212,24 @@ export class Node extends TypedEventEmitter<NodeEvents> {
'User-Id': this.manager.id
};

if (this.sessionId) headers['Session-Id'] = this.sessionId;
this.emit('debug', `[Socket] -> [${this.name}] : Connecting ${this.url}, Version: ${this.version}, Trying to resume? ${!!this.sessionId}`);
if (!this.initialized) this.initialized = true;
if (this.sessionId && this.manager.options.resume)
headers['Session-Id'] = this.sessionId;
if (!this.initialized)
this.initialized = true;

this.emit('debug', `[Socket] -> [${this.name}] : Connecting ${this.url}...`);

const url = new URL(`${this.url}${this.version}/websocket`);
const url = new URL(this.url);
this.ws = new Websocket(url.toString(), { headers } as Websocket.ClientOptions);

this.ws.once('upgrade', response => this.open(response));
this.ws.once('close', (...args) => this.close(...args));
this.ws.on('error', error => this.error(error));
this.ws.on('message', data => void this.message(data).catch(error => this.error(error as Error)));
}

/**
* Disconnect from lavalink
* Disconnect from Lavalink
* @param code Status code
* @param reason Reason for disconnect
*/
Expand All @@ -245,8 +244,8 @@ export class Node extends TypedEventEmitter<NodeEvents> {
* @internal
*/
private open(response: IncomingMessage): void {
const resumed = response.headers['session-resumed'] === 'true';
this.emit('debug', `[Socket] <-> [${this.name}] : Connection Handshake Done! ${this.url} | Upgrade Headers Resumed: ${resumed}`);
const resumed = response.headers['session-resumed'];
this.emit('debug', `[Socket] <-> [${this.name}] : Connection Handshake Done! ${this.url} | Resumed Header Value: ${resumed}`);

Check failure on line 248 in src/node/Node.ts

View workflow job for this annotation

GitHub Actions / build

Invalid type "string | string[] | undefined" of template literal expression
this.reconnects = 0;
this.state = State.NEARLY;
}
Expand Down Expand Up @@ -275,7 +274,7 @@ export class Node extends TypedEventEmitter<NodeEvents> {
this.sessionId = json.sessionId;

const players = [ ...this.manager.players.values() ].filter(player => player.node.name === this.name);
const resumeByLibrary = this.initialized && (players.length && this.manager.options.resumeByLibrary);
const resumeByLibrary = Boolean(this.initialized && (players.length && this.manager.options.resumeByLibrary));

if (!json.resumed && resumeByLibrary) {
try {
Expand All @@ -286,13 +285,14 @@ export class Node extends TypedEventEmitter<NodeEvents> {
}

this.state = State.CONNECTED;
this.emit('debug', `[Socket] -> [${this.name}] : Lavalink is ready! | Lavalink resume: ${json.resumed} | Lib resume: ${!!resumeByLibrary}`);
this.emit('ready', json.resumed || Boolean(resumeByLibrary));
this.emit('debug', `[Socket] -> [${this.name}] : Lavalink is ready! | Lavalink resume: ${json.resumed} | Lib resume: ${resumeByLibrary}`);
this.emit('ready', json.resumed, resumeByLibrary);

if (this.manager.options.resume) {
await this.rest.updateSession(this.manager.options.resume, this.manager.options.resumeTimeout);
this.emit('debug', `[Socket] -> [${this.name}] : Resuming configured!`);
}

break;
}
case OpCodes.EVENT:
Expand Down Expand Up @@ -355,7 +355,6 @@ export class Node extends TypedEventEmitter<NodeEvents> {
this.ws?.removeAllListeners();
this.ws?.close();
this.ws = null;
this.sessionId = null;
this.state = State.DISCONNECTED;
if (!this.shouldClean) return;
this.destroyed = true;
Expand Down

0 comments on commit e13511f

Please sign in to comment.