WebSocket
API reference for the custom WebSocket client in Moonlink.js
WebSocket Class
The WebSocket
class in Moonlink.js is a custom implementation of a WebSocket client, extending Node.js's EventEmitter
. It handles the low-level WebSocket protocol for communication with Lavalink servers.
This class is primarily used internally by Moonlink.js for node communication and is generally not intended for direct use by consumers of the library.
Properties
Property | Type | Description |
---|---|---|
url | URL | The URL of the WebSocket server. |
headers | Record<string, string> | Headers sent during the WebSocket handshake. |
connected | boolean | Indicates whether the WebSocket is currently connected. |
Methods
constructor
Constructor
Creates a new WebSocket instance and initiates the connection.
Parameters
ws://localhost:2333/v4/websocket
). Returns & Example
Returns
• WebSocket
// Used internally by Node class
// const socket = new WebSocket('ws://localhost:2333/v4/websocket', { headers: { Authorization: 'youshallnotpass' } });
::
send
Send Data
Sends data over the WebSocket connection.
Parameters
Returns & Example
Returns
• void
// Used internally by Node class
// socket.send(JSON.stringify({ op: 'play', /* ... */ }));
close
Close Connection
Closes the WebSocket connection.
Parameters
1000
''
Returns & Example
Returns
• void
// Used internally by Node class
// socket.close(1000, 'Normal Closure');
addEventListener
Add Event Listener
Registers an event listener for WebSocket events.
Parameters
open
, message
, close
, error
). once: true
for a one-time listener). Returns & Example
Returns
• void
// Used internally by Node class
// socket.addEventListener('message', (event) => console.log('Received:', event.data));
Events
The WebSocket
class extends EventEmitter
and emits the following events:
Event | Description | Parameters |
---|---|---|
open | Emitted when the WebSocket connection is established. | void |
message | Emitted when a message is received from the server. | `{ data: string |
close | Emitted when the WebSocket connection is closed. | { code: number, reason: string } |
error | Emitted when an error occurs. | { error: Error } |
ping | Emitted when a ping frame is received. | void |
pong | Emitted when a pong frame is received. | void |
Usage Example
Basic Usage
Example of how the WebSocket class is used internally by the Node.
Code Example
// This is an internal class, direct usage is generally not recommended.
// Example of how it's used within the Node class:
// In Node's connect method:
// this.socket = new WebSocket(
// `ws${this.secure ? "s" : ""}://${this.address}/${this.pathVersion}/websocket`,
// {
// headers: {
// Authorization: this.password,
// "User-Id": this.manager.options.clientId,
// "Client-Name": this.manager.options.clientName,
// },
// }
// );
// this.socket.addEventListener('open', this.open.bind(this), { once: true });
// this.socket.addEventListener('close', this.close.bind(this), { once: true });
// this.socket.addEventListener('message', this.message.bind(this));
// this.socket.addEventListener('error', this.error.bind(this));
// In Node's message method (handling incoming data):
// this.socket.send(JSON.stringify({ op: 'pong' }));
// In Node's close method:
// this.socket.close();