28 lines
691 B
JavaScript
28 lines
691 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);
|
||
|
|
}
|
||
|
|
}
|