@@ -46,8 +46,21 @@ function checkRateLimit(clientContext: ClientContext): boolean {
4646const PORT = Number ( process . env . WS_PORT ) || 3002 ;
4747const HOST = process . env . WS_HOST || "localhost" ;
4848
49- // Create HTTP server first
49+ const setCorsHeaders = ( res : any ) => {
50+ res . setHeader ( "Access-Control-Allow-Origin" , "*" ) ;
51+ res . setHeader ( "Access-Control-Allow-Methods" , "GET, OPTIONS" ) ;
52+ res . setHeader ( "Access-Control-Allow-Headers" , "Content-Type" ) ;
53+ } ;
54+
5055const server = createServer ( ( req : any , res : any ) => {
56+ setCorsHeaders ( res ) ;
57+
58+ if ( req . method === "OPTIONS" ) {
59+ res . writeHead ( 204 ) ;
60+ res . end ( ) ;
61+ return ;
62+ }
63+
5164 if ( req . url === "/health" && req . method === "GET" ) {
5265 res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
5366 res . end ( JSON . stringify ( { status : "ok" , timestamp : new Date ( ) . toISOString ( ) } ) ) ;
@@ -112,7 +125,7 @@ wss.on("connection", (ws: WebSocket, _req: any) => {
112125 } ;
113126
114127 clientContextMap . set ( ws , clientContext ) ;
115- // console.log(`✅ Client connected: ${clientId} from ${clientIp} (Total: ${clientContextMap.size})`);
128+ // console.log(`✅ Client connected: ${clientId} (Total: ${clientContextMap.size})`);
116129
117130 ws . on ( "pong" , ( ) => {
118131 clientContext . isAlive = true ;
@@ -156,10 +169,7 @@ wss.on("connection", (ws: WebSocket, _req: any) => {
156169
157170 ws . on ( "close" , ( ) => {
158171 clientContextMap . delete ( ws ) ;
159- // const duration = Date.now() - clientContext.connectedAt.getTime();
160- // console.log(
161- // `❌ Client disconnected: ${clientId} (connected for ${duration}ms, sent ${clientContext.messagesSent} messages, Total: ${clientContextMap.size})`,
162- // );
172+ // console.log(`❌ Client disconnected: ${clientId} (Total: ${clientContextMap.size})`);
163173 } ) ;
164174} ) ;
165175
@@ -170,6 +180,7 @@ const heartbeatInterval = setInterval(() => {
170180
171181 if ( ! clientContext . isAlive ) {
172182 console . warn ( `⏱️ Terminating inactive client: ${ clientContext . id } ` ) ;
183+ clientContextMap . delete ( ws ) ;
173184 ws . terminate ( ) ;
174185 return ;
175186 }
0 commit comments