@brainstack/bridge-server
Advanced tools
Comparing version 2.0.73 to 2.0.74
import WebSocket from 'ws'; | ||
import { Logger } from '@brainstack/log'; | ||
/** | ||
* A WebSocket server implementation that allows for bidirectional communication between clients. | ||
*/ | ||
declare class BridgeServer { | ||
private logger; | ||
/** | ||
* The port number that the WebSocket server will listen on. Defaults to 7777. | ||
*/ | ||
private port; | ||
/** | ||
* The hostname or IP address that the WebSocket server will listen on. Defaults to 'localhost'. | ||
*/ | ||
private host; | ||
/** | ||
* The WebSocket.Server instance that handles incoming connections. | ||
*/ | ||
wss: WebSocket.Server; | ||
/** | ||
* A Map that maps unique UUIDs to WebSocket connections. | ||
*/ | ||
private uuidToSocket; | ||
/** | ||
* A Map that maps WebSocket connections to unique UUIDs. | ||
*/ | ||
private socketToUUID; | ||
private onMessageCallback; | ||
/** | ||
* A callback function that is called whenever a message is received from a client. | ||
*/ | ||
onMessageCallback: (data: string, uuid: string) => void; | ||
/** | ||
* Creates a new instance of the BridgeServer class. | ||
* @param logger - An instance of the @brainstack/log logger. The log level can be specified by passing a number between 1 and 5, where 1 is errors only log level and 5 is verbose the most detailed log level. | ||
*/ | ||
constructor(logger?: Logger); | ||
/** | ||
* Sets the callback function that is called whenever a message is received from a client. | ||
* @param callback - The callback function to set. | ||
*/ | ||
onMessage(callback: (data: string, uuid: string) => void): void; | ||
/** | ||
* Broadcasts a message to all connected clients, excluding the sender. | ||
* @param sender - The WebSocket connection of the sender. | ||
* @param rawData - The message data to broadcast. | ||
*/ | ||
broadcast(sender: WebSocket, rawData: string): void; | ||
listen({ host, port }?: { | ||
host?: string; | ||
port?: number; | ||
}): void; | ||
/** | ||
* Starts the WebSocket server and begins listening for incoming connections. | ||
* The `options` parameter can either be an object with an `http.Server` property, or an object with `host` and `port` properties. | ||
* @param options - The options for starting the WebSocket server. | ||
*/ | ||
listen(options: any): void; | ||
/** | ||
* Called by the `listen()` method, this method sets up event listeners for incoming connections and handles cleaning up after a client disconnects. | ||
* @private | ||
*/ | ||
private _listen; | ||
/** | ||
* Removes a client's WebSocket connection and UUID from the `uuidToSocket` and `socketToUUID` Maps. | ||
* @param ws - The WebSocket connection to remove. | ||
* @param uuid - The UUID of the client to remove. | ||
*/ | ||
private cleanupConnection; | ||
/** | ||
* Gracefully shuts down the WebSocket server by closing all connections and then exiting the process. | ||
*/ | ||
gracefulShutdown(): void; | ||
/** | ||
* Generates a unique UUID for a new client connection. | ||
* @returns A unique UUID. | ||
*/ | ||
private generateUUID; | ||
} | ||
export { BridgeServer }; |
@@ -9,3 +9,10 @@ "use strict"; | ||
const log_1 = require("@brainstack/log"); | ||
/** | ||
* A WebSocket server implementation that allows for bidirectional communication between clients. | ||
*/ | ||
class BridgeServer { | ||
/** | ||
* Creates a new instance of the BridgeServer class. | ||
* @param logger - An instance of the @brainstack/log logger. The log level can be specified by passing a number between 1 and 5, where 1 is errors only log level and 5 is verbose the most detailed log level. | ||
*/ | ||
constructor(logger = (0, log_1.createLogger)(5)) { | ||
@@ -20,5 +27,14 @@ this.logger = logger; | ||
} | ||
/** | ||
* Sets the callback function that is called whenever a message is received from a client. | ||
* @param callback - The callback function to set. | ||
*/ | ||
onMessage(callback) { | ||
this.onMessageCallback = callback; | ||
} | ||
/** | ||
* Broadcasts a message to all connected clients, excluding the sender. | ||
* @param sender - The WebSocket connection of the sender. | ||
* @param rawData - The message data to broadcast. | ||
*/ | ||
broadcast(sender, rawData) { | ||
@@ -35,8 +51,26 @@ this.wss.clients.forEach((client) => { | ||
} | ||
listen({ host = this.host, port = this.port } = {}) { | ||
this._listen({ host, port }); | ||
/** | ||
* Starts the WebSocket server and begins listening for incoming connections. | ||
* The `options` parameter can either be an object with an `http.Server` property, or an object with `host` and `port` properties. | ||
* @param options - The options for starting the WebSocket server. | ||
*/ | ||
listen(options) { | ||
if (options === null || options === void 0 ? void 0 : options.server) { | ||
this.wss = new ws_1.default.Server({ server: options.server }); | ||
this._listen(); | ||
return; | ||
} | ||
if ((options === null || options === void 0 ? void 0 : options.host) && (options === null || options === void 0 ? void 0 : options.port)) { | ||
this.wss = new ws_1.default.Server({ host: options.host, port: options.port }); | ||
this._listen(); | ||
return; | ||
} | ||
throw Error("The listen() function has bad argument."); | ||
} | ||
_listen({ host, port }) { | ||
this.wss = new ws_1.default.Server({ port, host }); | ||
this.logger.info(`WebSocket server started on ws://${host}:${port}`); | ||
/** | ||
* Called by the `listen()` method, this method sets up event listeners for incoming connections and handles cleaning up after a client disconnects. | ||
* @private | ||
*/ | ||
_listen() { | ||
this.logger.info(`WebSocket server started on ws://${this.wss.options.host}:${this.wss.options.port}`); | ||
this.wss.on('connection', (ws) => { | ||
@@ -59,2 +93,7 @@ const uuid = this.generateUUID(); | ||
} | ||
/** | ||
* Removes a client's WebSocket connection and UUID from the `uuidToSocket` and `socketToUUID` Maps. | ||
* @param ws - The WebSocket connection to remove. | ||
* @param uuid - The UUID of the client to remove. | ||
*/ | ||
cleanupConnection(ws, uuid) { | ||
@@ -65,2 +104,5 @@ this.socketToUUID.delete(ws); | ||
} | ||
/** | ||
* Gracefully shuts down the WebSocket server by closing all connections and then exiting the process. | ||
*/ | ||
gracefulShutdown() { | ||
@@ -73,2 +115,6 @@ this.logger.info('Starting graceful shutdown of the BridgeServer.'); | ||
} | ||
/** | ||
* Generates a unique UUID for a new client connection. | ||
* @returns A unique UUID. | ||
*/ | ||
generateUUID() { | ||
@@ -75,0 +121,0 @@ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { |
{ | ||
"name": "@brainstack/bridge-server", | ||
"version": "2.0.73", | ||
"version": "2.0.74", | ||
"description": "Brainstack Bridge Server", | ||
@@ -31,8 +31,8 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@brainstack/hub": "^1.2.15", | ||
"@brainstack/log": "^1.1.116", | ||
"@brainstack/hub": "^1.2.16", | ||
"@brainstack/log": "^1.1.117", | ||
"ioredis": "^5.3.2", | ||
"ws": "^8.14.2" | ||
}, | ||
"gitHead": "2f4d78514bd9760f6f9224372fceb583d98aaaa8" | ||
"gitHead": "0b17ed2ff3614bf8cde969f927bbb24f0def6e45" | ||
} |
import WebSocket from 'ws'; | ||
import { createLogger, Logger } from '@brainstack/log'; | ||
/** | ||
* A WebSocket server implementation that allows for bidirectional communication between clients. | ||
*/ | ||
class BridgeServer { | ||
/** | ||
* The port number that the WebSocket server will listen on. Defaults to 7777. | ||
*/ | ||
private port: number; | ||
/** | ||
* The hostname or IP address that the WebSocket server will listen on. Defaults to 'localhost'. | ||
*/ | ||
private host: string; | ||
/** | ||
* The WebSocket.Server instance that handles incoming connections. | ||
*/ | ||
public wss: WebSocket.Server; | ||
/** | ||
* A Map that maps unique UUIDs to WebSocket connections. | ||
*/ | ||
private uuidToSocket: Map<string, WebSocket>; | ||
/** | ||
* A Map that maps WebSocket connections to unique UUIDs. | ||
*/ | ||
private socketToUUID: Map<WebSocket, string>; | ||
private onMessageCallback: (data: string, uuid: string) => void; | ||
/** | ||
* A callback function that is called whenever a message is received from a client. | ||
*/ | ||
public onMessageCallback: (data: string, uuid: string) => void; | ||
/** | ||
* Creates a new instance of the BridgeServer class. | ||
* @param logger - An instance of the @brainstack/log logger. The log level can be specified by passing a number between 1 and 5, where 1 is errors only log level and 5 is verbose the most detailed log level. | ||
*/ | ||
constructor(private logger: Logger = createLogger(5)) { | ||
@@ -21,2 +46,6 @@ this.port = Number(process.env.PORT || 7777); | ||
/** | ||
* Sets the callback function that is called whenever a message is received from a client. | ||
* @param callback - The callback function to set. | ||
*/ | ||
public onMessage(callback: (data: string, uuid: string) => void): void { | ||
@@ -26,2 +55,7 @@ this.onMessageCallback = callback; | ||
/** | ||
* Broadcasts a message to all connected clients, excluding the sender. | ||
* @param sender - The WebSocket connection of the sender. | ||
* @param rawData - The message data to broadcast. | ||
*/ | ||
public broadcast(sender: WebSocket, rawData: string): void { | ||
@@ -39,10 +73,30 @@ this.wss.clients.forEach((client) => { | ||
public listen({ host = this.host, port = this.port } = {}): void { | ||
this._listen({ host, port }); | ||
/** | ||
* Starts the WebSocket server and begins listening for incoming connections. | ||
* The `options` parameter can either be an object with an `http.Server` property, or an object with `host` and `port` properties. | ||
* @param options - The options for starting the WebSocket server. | ||
*/ | ||
public listen(options: any): void { | ||
if (options?.server){ | ||
this.wss = new WebSocket.Server({ server: options.server }); | ||
this._listen() | ||
return; | ||
} | ||
if (options?.host && options?.port){ | ||
this.wss = new WebSocket.Server({ host:options.host, port:options.port }); | ||
this._listen() | ||
return; | ||
} | ||
throw Error("The listen() function has bad argument.") | ||
} | ||
/** | ||
* Called by the `listen()` method, this method sets up event listeners for incoming connections and handles cleaning up after a client disconnects. | ||
* @private | ||
*/ | ||
private _listen(): void { | ||
this.logger.info(`WebSocket server started on ws://${this.wss.options.host}:${this.wss.options.port}`); | ||
private _listen({ host, port }): void { | ||
this.wss = new WebSocket.Server({ port, host }); | ||
this.logger.info(`WebSocket server started on ws://${host}:${port}`); | ||
this.wss.on('connection', (ws: WebSocket) => { | ||
@@ -69,2 +123,7 @@ const uuid = this.generateUUID(); | ||
/** | ||
* Removes a client's WebSocket connection and UUID from the `uuidToSocket` and `socketToUUID` Maps. | ||
* @param ws - The WebSocket connection to remove. | ||
* @param uuid - The UUID of the client to remove. | ||
*/ | ||
private cleanupConnection(ws: WebSocket, uuid: string): void { | ||
@@ -76,2 +135,5 @@ this.socketToUUID.delete(ws); | ||
/** | ||
* Gracefully shuts down the WebSocket server by closing all connections and then exiting the process. | ||
*/ | ||
public gracefulShutdown(): void { | ||
@@ -85,2 +147,6 @@ this.logger.info('Starting graceful shutdown of the BridgeServer.'); | ||
/** | ||
* Generates a unique UUID for a new client connection. | ||
* @returns A unique UUID. | ||
*/ | ||
private generateUUID(): string { | ||
@@ -87,0 +153,0 @@ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { |
158668
885
Updated@brainstack/hub@^1.2.16
Updated@brainstack/log@^1.1.117