36 lines
832 B
JavaScript
36 lines
832 B
JavaScript
/**
|
|
* Represents a connected WebSocket client
|
|
*/
|
|
export default class SocketClient {
|
|
#id;
|
|
#ws;
|
|
|
|
/**
|
|
* Create a new websocket server client instance.
|
|
* @param {string} id - The unique identifier of the client.
|
|
* @param {ServerWebSocket} - The Socket object for sending messages.
|
|
*/
|
|
constructor(id, ws) {
|
|
this.#id = id;
|
|
this.#ws = ws;
|
|
}
|
|
|
|
/**
|
|
* Sends a message to the client
|
|
* @param {*} message - The data to be sent to the client. Will be converted to string if not a string.
|
|
*/
|
|
send(message) {
|
|
if(typeof(message) != 'string') message = JSON.stringify(message);
|
|
|
|
this.#ws.send(message);
|
|
}
|
|
|
|
/**
|
|
* Gets the unique client id.
|
|
* @returns {string} - the unique client id
|
|
*/
|
|
id() {
|
|
return this.#id;
|
|
}
|
|
}
|