From 58b022f738619682197b559943e02b74cacb25d4 Mon Sep 17 00:00:00 2001 From: alma Date: Fri, 16 Jan 2026 01:32:30 +0100 Subject: [PATCH] refactor Notifications rc --- lib/services/rocketchat-call-listener.ts | 44 ++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/lib/services/rocketchat-call-listener.ts b/lib/services/rocketchat-call-listener.ts index 3d2a910..fb1a81a 100644 --- a/lib/services/rocketchat-call-listener.ts +++ b/lib/services/rocketchat-call-listener.ts @@ -30,11 +30,13 @@ export class RocketChatCallListener { private reconnectDelay = 3000; private isConnecting = false; private isConnected = false; + private isDdpConnected = false; // Track DDP connection state private callHandlers: Set = new Set(); private userId: string | null = null; private authToken: string | null = null; private baseUrl: string | null = null; private subscriptionId: string | null = null; + private connectId: string | null = null; private constructor() {} @@ -92,8 +94,10 @@ export class RocketChatCallListener { logger.debug('[ROCKETCHAT_CALL_LISTENER] WebSocket connected'); this.isConnecting = false; this.isConnected = true; + this.isDdpConnected = false; // Reset DDP connection state this.reconnectAttempts = 0; - this.authenticate(); + // First, establish DDP connection + this.connectDDP(); }; this.ws.onmessage = (event) => { @@ -163,7 +167,30 @@ export class RocketChatCallListener { } /** - * Authenticate with RocketChat using resume token + * Establish DDP connection (required before login) + */ + private connectDDP(): void { + if (!this.ws) { + return; + } + + this.connectId = `connect-${Date.now()}`; + const connectMessage = { + msg: 'connect', + version: '1', + support: ['1', 'pre2', 'pre1'], + }; + + console.log('[ROCKETCHAT_CALL_LISTENER] 🔗 Sending DDP connect message', { + connectId: this.connectId, + message: connectMessage, + }); + + this.ws.send(JSON.stringify(connectMessage)); + } + + /** + * Authenticate with RocketChat using resume token (after DDP is connected) */ private authenticate(): void { if (!this.ws || !this.authToken || !this.userId) { @@ -270,6 +297,17 @@ export class RocketChatCallListener { * Handle incoming WebSocket messages */ private handleMessage(message: any): void { + // Handle DDP connected message + if (message.msg === 'connected') { + console.log('[ROCKETCHAT_CALL_LISTENER] ✅ DDP connected!', { + session: message.session, + }); + this.isDdpConnected = true; + // Now we can authenticate + this.authenticate(); + return; + } + // Handle error messages if (message.msg === 'error') { const errorDetails = { @@ -539,7 +577,9 @@ export class RocketChatCallListener { this.isConnected = false; this.isConnecting = false; + this.isDdpConnected = false; this.subscriptionId = null; + this.connectId = null; this.callHandlers.clear(); logger.debug('[ROCKETCHAT_CALL_LISTENER] Disconnected');