UNPKG

@pulzar/core

Version:

Next-generation Node.js framework for ultra-fast web applications with zero-reflection DI, GraphQL, WebSockets, events, and edge runtime support

56 lines 1.64 kB
export class WebSocketAdapter { connectionHandlers = []; disconnectionHandlers = []; messageHandlers = []; errorHandlers = []; connections = new Map(); stats = { connections: 0, messagesSent: 0, messagesReceived: 0, errors: 0, bytesSent: 0, bytesReceived: 0, }; onConnection(handler) { this.connectionHandlers.push(handler); } onDisconnection(handler) { this.disconnectionHandlers.push(handler); } onMessage(handler) { this.messageHandlers.push(handler); } onError(handler) { this.errorHandlers.push(handler); } async send(connectionId, message) { const connection = this.connections.get(connectionId); if (!connection) { throw new Error(`Connection ${connectionId} not found`); } this.stats.messagesSent++; this.stats.bytesSent += JSON.stringify(message).length; } async disconnect(connectionId, reason) { const connection = this.connections.get(connectionId); if (connection) { this.connections.delete(connectionId); this.stats.connections--; for (const handler of this.disconnectionHandlers) { await handler(connection, reason); } } } async getStats() { return { ...this.stats }; } async start() { // Implementation would connect to WebSocket server } async stop() { // Implementation would disconnect from WebSocket server } } export default WebSocketAdapter; //# sourceMappingURL=ws.js.map