The WSClientRoom class extends WSClient to provide room-based functionality for WebSocket communication. It allows clients to create, join, and manage rooms with dedicated messaging and command systems. It's better to read the documentation for WSClient first to understand the basic WebSocket client features.
Creates a new WebSocket room client instance. Inherits all functionality from WSClient.
Parameters:
url(string, optional): The WebSocket server URL. Ifnull, automatically determined from current domain and scheme.defaultTimeout(number, optional): Default timeout in milliseconds for operations. Default:5000.
Example:
import { WSClientRoom } from 'wsmini';
const wsClient = new WSClientRoom('ws://localhost:8889');
await wsClient.connect();Creates a new room or joins an existing room with the specified name.
Parameters:
name(string, optional): The room name. Ifnull, server generates a name.data(object, optional): Additional data to send with the request. Default:{}.timeout(number, optional): Timeout in milliseconds. Default:defaultTimeout.
Returns: Promise<Room> - Resolves with a Room object when successful.
Example:
// Create or join a room with auto-generated name
const room = await wsClient.roomCreateOrJoin();
// Create or join a specific room
const room = await wsClient.roomCreateOrJoin('game-lobby');
// With additional data
const room = await wsClient.roomCreateOrJoin('chat-room', {
userInfo: { nickname: 'Player1' }
});Creates a new room with the specified name. Fails if room already exists.
Parameters:
name(string, optional): The room name. Ifnull, server generates a name.data(object, optional): Additional data to send with the request. Default:{}.timeout(number, optional): Timeout in milliseconds. Default:defaultTimeout.
Returns: Promise<Room> - Resolves with a Room object when successful.
Example:
// Create a room with auto-generated name
const room = await wsClient.roomCreate();
// Create a specific room
const room = await wsClient.roomCreate('my-private-room');
// Handle room creation errors
try {
const room = await wsClient.roomCreate('existing-room');
} catch (error) {
console.error('Room creation failed:', error.message);
}Joins an existing room. Fails if room doesn't exist.
Parameters:
name(string): The room name to join.data(object, optional): Additional data to send with the request. Default:{}.timeout(number, optional): Timeout in milliseconds. Default:defaultTimeout.
Returns: Promise<Room> - Resolves with a Room object when successful.
Example:
// Join an existing room
const room = await wsClient.roomJoin('game-lobby');
// Join with user data
const room = await wsClient.roomJoin('chat-room', {
userInfo: { nickname: 'Player2', avatar: 'avatar1.png' }
});Leaves a room and cleans up associated event listeners.
Parameters:
name(string): The room name to leave.timeout(number, optional): Timeout in milliseconds. Default:defaultTimeout.
Returns: Promise - Resolves when successfully left the room.
Example:
// Leave a specific room without waiting for confirmation
wsClient.roomLeave('game-lobby');
// Leave a room
await wsClient.roomLeave('game-lobby');Registers a callback for room list updates and gets the current room list.
Parameters:
callback(function): Function called with the room list.
Returns: Promise - Resolves with subscription confirmation.
Example:
// Get room list and listen for updates
wsClient.roomOnRooms((rooms) => {
console.log('Available rooms:', rooms);
updateRoomListUI(rooms);
});When you successfully join or create a room, you receive a Room object that provides convenient methods for room interaction.
name(string): The room namemeta(object): Room metadata from the serverclients(array): List of clients in the room (contains client IDs and metadata send by the server)wsClient(WSClientRoom): Reference to the parent WSClientRoom instance
Sends a message to the room.
Parameters:
data(object): The message data to send.
Example:
const room = await wsClient.roomJoin('chat-room');
room.send({ message: 'Hello chat room!' });Sends a command to the room (fire and forget). The command is processed by the server and can trigger specific actions. It will fail if the command is not recognized by the server.
Parameters:
cmd(string): The command name.data(object, optional): The command data. Default:{}.
Example:
const room = await wsClient.roomJoin('game-room');
room.sendCmd('ready', { checkboardSide: 'white' });Leaves the room. This will clean up all associated event listeners and remove the client from the room.
Example:
const room = await wsClient.roomJoin('temporary-room');
// Later...
room.leave();Registers a callback for room messages.
Parameters:
callback(function): Function called when messages are received.
Returns: function - A function for potential removal of the listener.
Example:
const room = await wsClient.roomJoin('chat-room');
const removeListener = room.onMessage((message) => {
console.log('Message in room:', message);
});
// Later, to remove this specific listener if needed (leaving the room will also remove all listeners)
removeListener();Registers a callback for specific room commands.
Parameters:
cmd(string): The command name to listen for.callback(function): Function called when the command is received.
Returns: function - A function for potential removal of the listener.
Example:
const room = await wsClient.roomJoin('game-room');
room.onCmd('game-over', (data) => console.log('Game over:', data));Registers a callback for client list updates and immediately calls it with current clients.
Parameters:
callback(function): Function called with the client list.
Returns: function - A function for potential removal of the listener.
Example:
const room = await wsClient.roomJoin('game-room');
room.onClients((clients) => {
console.log('Room has', clients.length, 'clients');
});If you dont want to use the room object, you can also use the following methods directly on the WSClientRoom instance.
But you will need to provide the room name explicitly.
Registers a callback for client list updates in a room.
Parameters:
name(string): The room name.callback(function): Function called with the client list.
Returns: function - A function for potential removal of the listener.
Example:
// Listen for client list changes
wsClient.roomOnClients('game-room', (clients) => {
console.log('Room clients:', clients);
updatePlayerListUI(clients);
});Sends a message to all clients in a room (fire-and-forget).
Parameters:
name(string): The room name.data(object, optional): The message data to send. Default:{}.
Example:
// Send a simple message
wsClient.roomSend('chat-room', { message: 'Hello everyone!' });
// Send structured data
wsClient.roomSend('game-room', {
type: 'player-move',
position: { x: 100, y: 200 },
timestamp: Date.now()
});Sends a command to the room (fire and forget). The command is processed by the server and can trigger specific actions. It will fail if the command is not recognized by the server.
Parameters:
name(string): The room name.cmd(string): The command name.data(object, optional): The command data to send. Default:{}.
Example:
// Send a command to start a game
wsClient.roomSendCmd('game-room', 'start-game', {
gameMode: 'competitive'
});Registers a callback for room messages.
Parameters:
name(string): The room name.callback(function): Function called when messages are received.
Returns: function - A function for potential removal of the listener.
Example:
// Listen for room messages
wsClient.roomOnMessage('chat-room', (message) => {
console.log('Room message:', message);
});
// Remove message listener
const removeListener = wsClient.roomOnMessage('game-room', handleGameMessage);
// Later...
removeListener();Registers a callback for specific room commands sent by the server.
Parameters:
name(string): The room name.cmd(string): The command name to listen for.callback(function): Function called when the command is received.
Returns: function - A function for potential removal of the listener.
Example:
// Listen for game start commands
wsClient.roomOnCmd('game-room', 'start-game', (data) => {
console.log('Game starting with mode:', data.gameMode);
});
// Listen for player actions
wsClient.roomOnCmd('game-room', 'player-action', (data) => {
handlePlayerAction(data.action, data.playerId);
});