| 12 | } |
| 13 | |
| 14 | async connect(connectPort: number): Promise<void> { |
| 15 | if (this.client) { |
| 16 | this.client.destroy(); // Clean up any existing connection |
| 17 | } |
| 18 | |
| 19 | this.client = new net.Socket(); |
| 20 | try { |
| 21 | // Await the connection |
| 22 | await new Promise<void>((resolve, reject) => { |
| 23 | this.client?.connect({ port: connectPort }, resolve); |
| 24 | this.client?.on("error", reject); |
| 25 | }); |
| 26 | |
| 27 | // Send initial message or perform initial action |
| 28 | this.client.write("hello"); |
| 29 | |
| 30 | // Listen for data and emit events |
| 31 | this.client.pipe(this.consumer).on("data", (data: Buffer) => { |
| 32 | this.emit("data", data); // Emit data for async processing |
| 33 | }); |
| 34 | |
| 35 | logger.info("Connected successfully"); |
| 36 | } catch (err) { |
| 37 | console.error("Connection error:", (err as Error).message); |
| 38 | this.client?.destroy(); |
| 39 | throw err; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | async waitForFirstData(): Promise<Buffer> { |
| 44 | if (!this.client) { |