UNPKG

@huddle01/web-core

Version:

The Huddle01 Javascript SDK offers a comprehensive suite of methods and event listeners that allow for seamless real-time audio and video communication with minimal coding required.

151 lines (148 loc) 4.73 kB
import { LocalPeer_default } from './chunk-IDSGLAHU.js'; import { Room_default } from './chunk-5B3BC5M6.js'; import { Socket_default, ESocketCloseCode } from './chunk-RS7KXM4R.js'; import { mainLogger, setLogLevel } from './chunk-TOCFOGTC.js'; // src/HuddleClient.ts var logger = mainLogger.createSubLogger("HuddleClient"); var HuddleClient = class { /** * Connection Manager Instance, Hanlder socket connection and stores information about the connection */ __socket; /** * Room Instance, Handles the room and its connection */ __room; /** * Local Peer Instance, Handles the local peer and its connection */ __localPeer; /** * Project Id of the Huddle01 Project */ projectId; /** * Returns the underlying socket connection * @throws { Error } If the socket connection is not initialized */ get socket() { if (!this.__socket) { throw new Error( "Socket Is Not Initialized, You need to connect to the Huddle01 Socket Servers first" ); } return this.__socket; } /** * Returns the room instance, throws an error if the room is not created * @throws { Error } If the room is not created, Call createRoom() method before you can access the room */ get room() { return this.__room; } get localPeer() { return this.__localPeer; } /** * Room Id of the current room */ get roomId() { return this.room.roomId; } /** * Set a new region for the Huddle01 Media Servers */ setRegion = (region) => { logger.info("Setting a new region, ", region); this.socket.setRegion(region); }; constructor(data) { if (data.options?.logging) { setLogLevel("trace"); } logger.info("\u2705 Initializing HuddleClient"); this.projectId = data.projectId; this.__socket = Socket_default.create(data.options?.wsPolyfill, { huddle_api_url: data.options?.huddle_api_url }); this.__room = Room_default.create({ autoConsume: data.options?.autoConsume, activeSpeakers: data.options?.activeSpeakers }); this.__localPeer = LocalPeer_default.create({ handlerFactory: data.options?.handlerFactory, volatileMessages: data.options?.volatileMessaging }); this.__socket.on("closed", (code) => { logger.info( "\u{1F50C} Socket Connection closed, closing the room and LocalPeer" ); if (code === ESocketCloseCode.ROOM_CLOSED) { this.room.close("CLOSED"); } else if (code === ESocketCloseCode.ROOM_ENTRY_DENIED) { this.room.close("DENIED"); } else if (code === ESocketCloseCode.CONNECTION_EXPIRED) { logger.info("\u{1F514} Room closed due to connection expired"); this.room.close("CONNECTION_EXPIRED"); } else if (code === ESocketCloseCode.KICKED) { this.room.close("KICKED"); } else if (code === ESocketCloseCode.MAX_PEERS_REACHED) { logger.info("\u{1F514} Room closed due to max peers reached"); this.room.close("MAX_PEERS_REACHED"); } else if (code === ESocketCloseCode.ROOM_EXPIRED) { logger.info("\u{1F514} Room closed due to room expired"); this.room.close("ROOM_EXPIRED"); } else { this.room.close(); } this.localPeer.close(); }); } /** * Default method to connect to the Huddle01 Media Room. * * This method connects to socket, creates a room, and then connects to the room; */ joinRoom = async (data) => { logger.info("\u{1F514} Joining the room with roomId,", data.roomId); if (this.socket.connectionState === "connecting") { logger.warn( "\u{1F514} Socket is already connecting, waiting for the connection to be established" ); return this.room; } if (this.room.state === "connecting") { logger.warn("\u{1F514} Room join already in progress"); return this.room; } if (this.localPeer.joined) { logger.warn("\u{1F514} Already joined the room"); return this.room; } try { await this.socket.connect({ token: data.token }); const room = this.room.connect({ roomId: data.roomId }); return room; } catch (error) { logger.error("\u{1F534} Error While Joining the Room"); logger.error(error); throw error; } }; /** * Leave the room and disconnect from the socket */ leaveRoom = () => { logger.info("Leaving the room"); this.socket.close(ESocketCloseCode.NORMAL_CLOSURE); }; /** * Close the room and disconnect from the socket */ closeRoom = () => { logger.info("Closing the room"); this.socket.publish("closeRoom", void 0); }; }; var HuddleClient_default = HuddleClient; export { HuddleClient_default };