fuga
Version:
A comprehensive, feature-rich, and modern Lavalink v4 client for Node.js
104 lines • 3.65 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RaftLinkNode = void 0;
const events_1 = require("events");
const ws_1 = __importDefault(require("ws"));
const RestManager_1 = require("../rest/RestManager");
/**
* Represents a single connection to a Lavalink server node.
*/
class RaftLinkNode extends events_1.EventEmitter {
manager;
rest;
options;
connected = false;
sessionId = null;
stats = {};
ws = null;
reconnectTimeout = null;
retryInterval;
constructor(manager, options) {
super();
this.manager = manager;
this.options = { resumeTimeout: 60, ...options };
this.retryInterval = options.retryInterval ?? 5000;
this.rest = new RestManager_1.RestManager(this);
}
connect() {
if (this.connected || this.ws)
return;
const headers = {
Authorization: this.options.password,
'User-Id': this.manager.userId ?? undefined,
'Client-Name': `RaftLink/${require('../../package.json').version}`,
};
if (this.sessionId)
headers['Resume-Key'] = this.sessionId;
const url = `ws${this.options.secure ? 's' : ''}://${this.options.host}:${this.options.port}/v4/websocket`;
this.ws = new ws_1.default(url, { headers: Object.fromEntries(Object.entries(headers).filter(([, v]) => v !== undefined)) });
this.ws.on('open', this.onOpen.bind(this));
this.ws.on('message', this.onMessage.bind(this));
this.ws.on('error', this.onError.bind(this));
this.ws.on('close', this.onClose.bind(this));
}
disconnect() {
if (!this.connected)
return;
this.ws?.close(1000, 'Manually disconnected');
}
onOpen() {
if (this.reconnectTimeout)
clearTimeout(this.reconnectTimeout);
this.emit('connect');
}
onMessage(data) {
const payload = JSON.parse(data.toString());
switch (payload.op) {
case 'ready':
this.onReady(payload);
break;
case 'stats':
this.stats = payload;
this.emit('stats', payload);
break;
case 'playerUpdate':
this.emit('playerUpdate', payload);
break;
case 'event':
this.emit('event', payload);
break;
default:
this.emit('raw', payload);
break;
}
}
onError(err) { this.emit('error', err); }
onClose(code, reason) {
this.connected = false;
this.ws = null;
this.emit('disconnect', code, reason.toString());
if (code === 1006)
console.warn(`[RaftLink] [Node] WebSocket closed with code 1006: Abnormal closure. Reconnecting...`);
if (code !== 1000 || reason.toString() !== 'Manually disconnected')
this.reconnect();
}
onReady(payload) {
this.connected = true;
this.sessionId = payload.sessionId;
this.emit('ready', payload);
if (payload.resumed)
return;
this.rest.updateSession(true, this.options.resumeTimeout);
}
reconnect() {
if (this.reconnectTimeout)
return;
this.emit('reconnecting');
this.reconnectTimeout = setTimeout(() => this.connect(), this.retryInterval);
}
}
exports.RaftLinkNode = RaftLinkNode;
//# sourceMappingURL=RaftLinkNode.js.map