@brainstack/bridge-server
Advanced tools
Comparing version 2.0.69 to 2.0.70
@@ -10,5 +10,7 @@ import WebSocket from 'ws'; | ||
private socketToUUID; | ||
private onMessageCallback; | ||
constructor(logger?: Logger); | ||
onMessage(callback: (data: string, uuid: string) => void): void; | ||
broadcast(sender: WebSocket, rawData: string): void; | ||
listen({ host, port }: { | ||
listen({ host, port }?: { | ||
host?: string; | ||
@@ -18,2 +20,3 @@ port?: number; | ||
private _listen; | ||
private cleanupConnection; | ||
gracefulShutdown(): void; | ||
@@ -20,0 +23,0 @@ private generateUUID; |
@@ -9,13 +9,15 @@ "use strict"; | ||
const log_1 = require("@brainstack/log"); | ||
const PORT = Number(process.env.PORT || 7777); | ||
const HOST = process.env.HOST || 'localhost'; | ||
class BridgeServer { | ||
constructor(logger = (0, log_1.createLogger)(5)) { | ||
this.logger = logger; | ||
this.port = PORT; | ||
this.host = HOST; | ||
this.port = Number(process.env.PORT || 7777); | ||
this.host = process.env.HOST || 'localhost'; | ||
this.uuidToSocket = new Map(); | ||
this.socketToUUID = new Map(); | ||
this.wss = null; | ||
this.onMessageCallback = () => { }; | ||
} | ||
onMessage(callback) { | ||
this.onMessageCallback = callback; | ||
} | ||
broadcast(sender, rawData) { | ||
@@ -32,8 +34,8 @@ this.wss.clients.forEach((client) => { | ||
} | ||
listen({ host = this.host, port = this.port }) { | ||
listen({ host = this.host, port = this.port } = {}) { | ||
this._listen({ host, port }); | ||
this.logger.info(`WebSocket server started on ws://${host}:${port}`); | ||
} | ||
_listen({ host, port }) { | ||
this.wss = new ws_1.default.Server({ port, host }); | ||
this.logger.info(`WebSocket server started on ws://${host}:${port}`); | ||
this.wss.on('connection', (ws) => { | ||
@@ -43,19 +45,20 @@ const uuid = this.generateUUID(); | ||
this.socketToUUID.set(ws, uuid); | ||
this.logger.verbose(`New client connection established with UUID: ${uuid}`); | ||
ws.on('message', (rawData) => { | ||
this.logger.verbose('Broadcasting raw data', rawData.toString()); | ||
this.broadcast(ws, rawData.toString()); | ||
this.logger.verbose(`Message received from UUID: ${uuid}:`, rawData.toString()); | ||
this.onMessageCallback(rawData.toString(), uuid); | ||
}); | ||
ws.on('close', () => { | ||
this.socketToUUID.delete(ws); | ||
this.uuidToSocket.delete(uuid); | ||
this.logger.verbose(`Client connection closed with UUID: ${uuid}`); | ||
this.cleanupConnection(ws, uuid); | ||
}); | ||
ws.on('error', (error) => { | ||
this.socketToUUID.delete(ws); | ||
this.uuidToSocket.delete(uuid); | ||
this.logger.error(`Error on client connection with UUID: ${uuid}:`, error); | ||
this.cleanupConnection(ws, uuid); | ||
}); | ||
}); | ||
} | ||
cleanupConnection(ws, uuid) { | ||
this.socketToUUID.delete(ws); | ||
this.uuidToSocket.delete(uuid); | ||
this.logger.verbose(`Client connection closed with UUID: ${uuid}`); | ||
} | ||
gracefulShutdown() { | ||
@@ -62,0 +65,0 @@ this.logger.info('Starting graceful shutdown of the BridgeServer.'); |
{ | ||
"name": "@brainstack/bridge-server", | ||
"version": "2.0.69", | ||
"version": "2.0.70", | ||
"description": "Brainstack Bridge Server", | ||
@@ -31,8 +31,8 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@brainstack/hub": "^1.2.11", | ||
"@brainstack/log": "^1.1.112", | ||
"@brainstack/hub": "^1.2.12", | ||
"@brainstack/log": "^1.1.113", | ||
"ioredis": "^5.3.2", | ||
"ws": "^8.14.2" | ||
}, | ||
"gitHead": "469bbde364d84f5a31034d80c4ec46716f69d58f" | ||
"gitHead": "ba97635e414bfac44d55cf0bbe62fc67d1994357" | ||
} |
import WebSocket from 'ws'; | ||
import { createLogger, Logger } from '@brainstack/log'; | ||
const PORT = Number(process.env.PORT || 7777); | ||
const HOST = process.env.HOST || 'localhost'; | ||
class BridgeServer { | ||
@@ -13,13 +10,17 @@ private port: number; | ||
private socketToUUID: Map<WebSocket, string>; | ||
private onMessageCallback: (data: string, uuid: string) => void; | ||
constructor( | ||
private logger: Logger = createLogger(5) | ||
) { | ||
this.port = PORT; | ||
this.host = HOST; | ||
constructor(private logger: Logger = createLogger(5)) { | ||
this.port = Number(process.env.PORT || 7777); | ||
this.host = process.env.HOST || 'localhost'; | ||
this.uuidToSocket = new Map(); | ||
this.socketToUUID = new Map(); | ||
this.wss = null; | ||
this.onMessageCallback = () => {}; | ||
} | ||
public onMessage(callback: (data: string, uuid: string) => void): void { | ||
this.onMessageCallback = callback; | ||
} | ||
public broadcast(sender: WebSocket, rawData: string): void { | ||
@@ -37,5 +38,4 @@ this.wss.clients.forEach((client) => { | ||
public listen({ host = this.host, port = this.port }): void { | ||
public listen({ host = this.host, port = this.port } = {}): void { | ||
this._listen({ host, port }); | ||
this.logger.info(`WebSocket server started on ws://${host}:${port}`); | ||
} | ||
@@ -45,2 +45,3 @@ | ||
this.wss = new WebSocket.Server({ port, host }); | ||
this.logger.info(`WebSocket server started on ws://${host}:${port}`); | ||
@@ -52,24 +53,14 @@ this.wss.on('connection', (ws: WebSocket) => { | ||
this.logger.verbose( | ||
`New client connection established with UUID: ${uuid}` | ||
); | ||
ws.on('message', (rawData) => { | ||
this.logger.verbose('Broadcasting raw data', rawData.toString()); | ||
this.broadcast(ws, rawData.toString()) | ||
this.logger.verbose(`Message received from UUID: ${uuid}:`, rawData.toString()); | ||
this.onMessageCallback(rawData.toString(), uuid); | ||
}); | ||
ws.on('close', () => { | ||
this.socketToUUID.delete(ws); | ||
this.uuidToSocket.delete(uuid); | ||
this.logger.verbose(`Client connection closed with UUID: ${uuid}`); | ||
this.cleanupConnection(ws, uuid); | ||
}); | ||
ws.on('error', (error) => { | ||
this.socketToUUID.delete(ws); | ||
this.uuidToSocket.delete(uuid); | ||
this.logger.error( | ||
`Error on client connection with UUID: ${uuid}:`, | ||
error | ||
); | ||
this.logger.error(`Error on client connection with UUID: ${uuid}:`, error); | ||
this.cleanupConnection(ws, uuid); | ||
}); | ||
@@ -79,3 +70,9 @@ }); | ||
public gracefulShutdown() { | ||
private cleanupConnection(ws: WebSocket, uuid: string): void { | ||
this.socketToUUID.delete(ws); | ||
this.uuidToSocket.delete(uuid); | ||
this.logger.verbose(`Client connection closed with UUID: ${uuid}`); | ||
} | ||
public gracefulShutdown(): void { | ||
this.logger.info('Starting graceful shutdown of the BridgeServer.'); | ||
@@ -89,9 +86,6 @@ this.wss.close(() => { | ||
private generateUUID(): string { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( | ||
/[xy]/g, | ||
function (c) { | ||
const r = (Math.random() * 16) | 0; | ||
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16); | ||
} | ||
); | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
const r = (Math.random() * 16) | 0; | ||
return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16); | ||
}); | ||
} | ||
@@ -98,0 +92,0 @@ } |
151795
Updated@brainstack/hub@^1.2.12
Updated@brainstack/log@^1.1.113