1- import { createCanProcessor , formatCanId } from '../utils/canProcessor' ;
2- import { loggingService } from './LoggingService' ;
1+ import { createCanProcessor } from '../utils/canProcessor' ;
32
43export type MessageHandler = ( data : any ) => void ;
54
5+ export interface ConnectionStatus {
6+ connected : boolean ;
7+ url : string ;
8+ error ?: string ;
9+ }
10+
611export class WebSocketService {
712 private ws : WebSocket | null = null ;
813 private processor : any = null ;
914 private reconnectAttempts = 0 ;
1015 private maxReconnectAttempts = 5 ;
1116 private reconnectDelay = 2000 ;
12- private messageCount = 0 ;
17+ private messageCount = 0 ;
1318 private messageListeners = new Map < string , Set < MessageHandler > > ( ) ;
1419 private suppressIngestion = false ;
1520
1621 async initialize ( ) {
22+ this . disconnect ( ) ;
23+
1724 try {
18- this . processor = await createCanProcessor ( ) ;
19- console . log ( 'CAN processor initialized' ) ;
25+ if ( ! this . processor ) {
26+ this . processor = await createCanProcessor ( ) ;
27+ console . log ( '[WebSocket] CAN processor initialized' ) ;
28+ }
2029 } catch ( error ) {
21- console . error ( 'Failed to initialize CAN processor:' , error ) ;
30+ console . error ( '[WebSocket] Failed to initialize CAN processor:' , error ) ;
2231 return ;
2332 }
2433 this . connect ( ) ;
@@ -46,58 +55,66 @@ export class WebSocketService {
4655 }
4756 }
4857
49- console . log ( `Connecting to WebSocket : ${ wsUrl } ` ) ;
58+ console . log ( `[WebSocket] Connecting to: ${ wsUrl } ` ) ;
5059
5160 try {
5261 this . ws = new WebSocket ( wsUrl ) ;
5362
5463 this . ws . onopen = ( ) => {
55- console . log ( 'WebSocket connected ' ) ;
64+ console . log ( '[ WebSocket] Connected ' ) ;
5665 this . reconnectAttempts = 0 ;
5766 this . messageCount = 0 ;
67+ this . notify ( 'status' , { connected : true , url : wsUrl } ) ;
5868 } ;
5969
6070 this . ws . onmessage = ( event ) => {
6171 try {
72+ // Milestone and initial message logging (replicated from original pecan)
73+ if ( this . messageCount < 3 ) {
74+ console . log ( `[WebSocket] Message #${ this . messageCount + 1 } :` , event . data ) ;
75+ }
76+ if ( this . messageCount === 10 || this . messageCount === 100 || ( this . messageCount > 0 && this . messageCount % 1000 === 0 ) ) {
77+ console . log ( `[WebSocket] Received ${ this . messageCount } messages` ) ;
78+ }
79+
6280 const messageData = JSON . parse ( event . data ) ;
6381
64- if ( messageData && typeof messageData === 'object' && messageData . type ) {
65- const listeners = this . messageListeners . get ( messageData . type ) ;
66- if ( listeners ) {
67- listeners . forEach ( handler => handler ( messageData ) ) ;
68- }
82+ if ( messageData ?. type ) {
83+ this . notify ( messageData . type , messageData ) ;
6984 }
85+ this . notify ( 'raw' , messageData ) ;
7086
7187 const decoded = this . processor . processWebSocketMessage ( messageData ) ;
72- if ( ! decoded ) return ;
73-
74- this . messageCount ++ ;
75- const messages = Array . isArray ( decoded ) ? decoded : [ decoded ] ;
76-
77- messages . forEach ( msg => {
78- if ( msg ?. signals ) {
79- if ( this . suppressIngestion ) return ;
80- loggingService . logFrame ( msg . time || Date . now ( ) , msg . canId , msg . data ) ;
88+ if ( decoded ) {
89+ if ( this . messageCount < 3 ) {
90+ console . log ( `[WebSocket] Decoded message(s) #${ this . messageCount + 1 } :` , decoded ) ;
8191 }
82- } ) ;
92+ this . notify ( 'decoded' , decoded ) ;
93+ }
94+
95+ this . messageCount ++ ;
8396 } catch ( error ) {
84- console . error ( 'Error processing WebSocket message:' , error ) ;
97+ console . error ( '[WebSocket] Error processing message:' , error ) ;
8598 }
8699 } ;
87100
88101 this . ws . onerror = ( error ) => {
89- console . error ( 'WebSocket error:' , error ) ;
102+ console . error ( '[WebSocket] Error:' , error ) ;
103+ this . notify ( 'status' , { connected : false , url : wsUrl , error : 'Connection error' } ) ;
90104 } ;
91105
92106 this . ws . onclose = ( event ) => {
93- console . log ( 'WebSocket disconnected' ) ;
107+ console . log ( '[WebSocket] Disconnected' ) ;
108+ this . notify ( 'status' , { connected : false , url : wsUrl , error : `Socket closed (code: ${ event . code } )` } ) ;
109+
94110 if ( event . code !== 1000 && this . reconnectAttempts < this . maxReconnectAttempts ) {
95111 this . reconnectAttempts ++ ;
96112 setTimeout ( ( ) => this . connect ( ) , this . reconnectDelay * this . reconnectAttempts ) ;
97113 }
98114 } ;
99115 } catch ( error ) {
100- console . error ( 'WebSocket error:' , error ) ;
116+ console . error ( '[WebSocket] Connection failed:' , error ) ;
117+ this . notify ( 'status' , { connected : false , url : wsUrl , error : String ( error ) } ) ;
101118 }
102119 }
103120
@@ -118,16 +135,36 @@ export class WebSocketService {
118135 this . messageListeners . get ( type ) ?. delete ( handler ) ;
119136 }
120137
138+ private notify ( type : string , data : any ) {
139+ const listeners = this . messageListeners . get ( type ) ;
140+ if ( listeners ) {
141+ listeners . forEach ( handler => handler ( data ) ) ;
142+ }
143+ }
144+
121145 disconnect ( ) {
122146 if ( this . ws ) {
123- this . ws . close ( 1000 , 'Client disconnect' ) ;
147+ this . ws . close ( 1000 , 'Manual disconnect' ) ;
124148 this . ws = null ;
125149 }
126150 }
127151
128152 isConnected ( ) : boolean {
129153 return this . ws !== null && this . ws . readyState === WebSocket . OPEN ;
130154 }
155+
156+ public setSuppressIngestion ( suppress : boolean ) {
157+ console . log ( `[WebSocket] Ingestion suppression: ${ suppress } ` ) ;
158+ this . suppressIngestion = suppress ;
159+ }
160+
161+ public isIngestionSuppressed ( ) : boolean {
162+ return this . suppressIngestion ;
163+ }
164+
165+ public getMessageCount ( ) : number {
166+ return this . messageCount ;
167+ }
131168}
132169
133170export const webSocketService = new WebSocketService ( ) ;
0 commit comments