@underpostnet/underpost
Version:
Underpost Platform — end-to-end CI/CD and application-delivery toolchain CLI. Covers bare metal, Kubernetes, K3s, kubeadm, LXD, container/image orchestration, secrets, databases, cron jobs, monitoring, SSH, runners, PWA + Workbox delivery, and release orc
91 lines (78 loc) • 2.99 kB
JavaScript
/**
* Stream WebSocket channel — manages per-socket room membership and user tracking.
* @module ws/core/channels/core.ws.stream
*/
import { IoChannel } from '../../IoInterface.js';
/**
* @class CoreWsStreamChannel
* @classdesc Manages the stream WebSocket channel. Each socket can join a room
* and broadcast connection/disconnection events to other room members.
*/
class CoreWsStreamChannel {
/** @type {Object.<string, Object.<string, Array>>} Per-socket room/user args keyed by `[hostKeyContext][socketId]`. */
static
/** @type {IoChannel} */
static
channel: 'stream',
stream: true,
controller(socket, client, payload, hostKeyContext, args) {
const [roomId, userId] = args;
// Collect existing users in the room before registering the new one
const existingUsers = [];
for (const entry of Object.values(CoreWsStreamChannel.
if (entry[0] === roomId && entry[1]) existingUsers.push(entry[1]);
}
CoreWsStreamChannel.
socket.join(roomId);
socket.to(roomId).emit('stream-user-connected', userId);
// Tell the joining user about everyone already in the room
if (existingUsers.length > 0) socket.emit('stream-existing-users', existingUsers);
},
connection(socket, client, hostKeyContext) {
CoreWsStreamChannel.
},
disconnect(socket, client, reason, hostKeyContext) {
const entry = CoreWsStreamChannel.
if (!entry || entry.length === 0) {
if (CoreWsStreamChannel.
return;
}
const [roomId, userId] = entry;
socket.to(roomId).emit('stream-user-disconnected', userId);
delete CoreWsStreamChannel.
},
});
/** @returns {Object.<string, import('socket.io').Socket>} Connected sockets map. */
static get client() {
return this.
}
/** @returns {string} Channel name. */
static get channel() {
return this.
}
/**
* Initializes state for a server instance.
* @param {string} hostKeyContext - Unique server context ID (`${host}${path}`).
*/
static init(hostKeyContext) {
this.
}
/**
* Registers a socket connection.
* @param {import('socket.io').Socket} socket
* @param {string} hostKeyContext
*/
static connection(socket, hostKeyContext) {
return this.
}
/**
* Handles socket disconnection.
* @param {import('socket.io').Socket} socket
* @param {string} reason
* @param {string} hostKeyContext
*/
static disconnect(socket, reason, hostKeyContext) {
return this.
}
}
export { CoreWsStreamChannel };