| 
					
				 | 
			
			
				@@ -1,14 +1,89 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import { Inject, Injectable } from '@nestjs/common'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import { ClientProxy } from '@nestjs/microservices'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import { interval } from 'rxjs'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import * as net from 'net' 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import { ConfigService } from '@nestjs/config'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Injectable() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 export class FisVerificationService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  constructor(@Inject(`SAMPLEAPP_SERVICE`) private client: ClientProxy) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  constructor( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Inject(`SAMPLEAPP_SERVICE`) private sampleClient: ClientProxy, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Inject(`JAVA_AFIS`) private javaClient: ClientProxy, // this one cannot work unfortunately 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private configService: ConfigService 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  ) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     // logic here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     setTimeout(() => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      this.client.emit(`message`, `Verification says HI`) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      // logic here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     }, 5000) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    interval(3000).subscribe(interval => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      this.sampleClient.emit(`message`, `Verification says HI`) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      // this.javaClient.emit(`message`, `Testing Message  from Verification`) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      // this.javaClient.send(`message`, `Testing Message  from Verification`) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    this.tryThis() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  tryThis() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const port = this.configService.get<number>('afis.tcpPort')!; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const host = this.configService.get<string>('afis.host')!; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const client = new net.Socket(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    const connectToServer = () => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      client.connect(port, host, () => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        console.log(`✅ Connected to Afis Java Server at ${host}:${port}`); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // Attempt to connect initially 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    connectToServer(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // Reconnect logic: try again after delay 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    client.on('error', (err) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      console.error(`❌ TCP Error:`, err.message); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      // Avoid crash on ECONNREFUSED etc. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      setTimeout(connectToServer, 3000); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    client.on('close', (hadError) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      console.warn(`⚠️ Connection closed${hadError ? ' due to error' : ''}. Reconnecting...`); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      setTimeout(connectToServer, 3000); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    client.on('end', () => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      console.warn('⚠️ Server closed the connection.'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // Optional: handle incoming data 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    client.on('data', (data) => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      console.log('📩 Received from server:', data.toString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // Message sending interval 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    interval(3000).subscribe(() => { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      if (client.destroyed) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        console.warn('⚠️ Cannot send: socket is destroyed.'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      const message = JSON.stringify({ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        pattern: 'get-user', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        data: { id: 1 }, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      client.write(message + '\n'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/* Reason based on ChatGPT: Why ClientProxy.send() or emit() won't work  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+The NestJS ClientProxy (when configured for Transport.TCP) uses its own custom binary protocol under the hood. This includes: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+A message envelope that wraps your pattern and data. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+Message IDs and event headers. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+A specific serialization/deserialization logic that is not plain JSON. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+NO newline character at the end. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+So when your Java server, which expects a newline-terminated JSON message, receives this custom binary format, it gets confused or sees null.*/ 
			 |