@@ -13,6 +13,9 @@ export class WebSocketService {
1313 private errorHandlers : ( ( error : Error ) => void ) [ ] = [ ] ;
1414 private isReconnecting = false ;
1515 private connectionTimeout : NodeJS . Timeout | null = null ;
16+ private connectionStartTime = 0 ;
17+ private messageBuffer : Message [ ] = [ ] ;
18+ private bufferTimeout : NodeJS . Timeout | null = null ;
1619
1720 public getSessionId ( ) : string {
1821 console . debug ( '[WebSocket] Getting session ID:' , this . sessionId ) ;
@@ -192,6 +195,7 @@ export class WebSocketService {
192195 console . log ( '[WebSocket] Connection established successfully' ) ;
193196 this . reconnectAttempts = 0 ;
194197 this . isReconnecting = false ;
198+ this . connectionStartTime = Date . now ( ) ;
195199 this . connectionHandlers . forEach ( handler => handler ( true ) ) ;
196200 if ( this . connectionTimeout ) {
197201 clearTimeout ( this . connectionTimeout ) ;
@@ -200,6 +204,9 @@ export class WebSocketService {
200204 } ;
201205 this . ws . onmessage = ( event ) => {
202206 this . debugLog ( 'Message received' ) ;
207+ const currentTime = Date . now ( ) ;
208+ const timeSinceConnection = currentTime - this . connectionStartTime ;
209+ const shouldBuffer = timeSinceConnection < 10000 ; // First 10 seconds
203210 // Find the first two comma positions to extract id and version
204211 const firstComma = event . data . indexOf ( ',' ) ;
205212 const secondComma = event . data . indexOf ( ',' , firstComma + 1 ) ;
@@ -241,11 +248,30 @@ export class WebSocketService {
241248 console . log ( '[WebSocket] Processing HTML message' ) ;
242249 }
243250
244- this . messageHandlers . forEach ( ( handler ) => handler ( message ) ) ;
251+ if ( shouldBuffer ) {
252+ this . messageBuffer . push ( message ) ;
253+ if ( this . bufferTimeout ) {
254+ clearTimeout ( this . bufferTimeout ) ;
255+ }
256+ this . bufferTimeout = setTimeout ( ( ) => {
257+ const messages = [ ...this . messageBuffer ] ;
258+ this . messageBuffer = [ ] ;
259+ messages . forEach ( msg => {
260+ this . messageHandlers . forEach ( handler => handler ( msg ) ) ;
261+ } ) ;
262+ } , 1000 ) ;
263+ } else {
264+ this . messageHandlers . forEach ( ( handler ) => handler ( message ) ) ;
265+ }
245266 } ;
246267
247268 this . ws . onclose = ( ) => {
248269 console . log ( '[WebSocket] Connection closed, stopping heartbeat' ) ;
270+ if ( this . bufferTimeout ) {
271+ clearTimeout ( this . bufferTimeout ) ;
272+ this . bufferTimeout = null ;
273+ }
274+ this . messageBuffer = [ ] ;
249275 this . stopHeartbeat ( ) ;
250276 this . connectionHandlers . forEach ( handler => handler ( false ) ) ;
251277 if ( ! this . isReconnecting ) {
0 commit comments