MCPcopy Create free account
hub / github.com/AutoCodeStack/ios-device-farm / WdaStreamClient

Class WdaStreamClient

src/modules/wda-stream/wda.stream.ts:6–78  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import logger from "../../config/logger";
5
6class WdaStreamClient extends EventEmitter {
7 private client: net.Socket | null = null;
8 private consumer = new MjpegParser();
9
10 constructor() {
11 super(); // Call the EventEmitter constructor
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) {
45 throw new Error("Client is not connected");
46 }
47
48 return new Promise<Buffer>((resolve, reject) => {
49 this.once("data", resolve); // Resolve with the first data chunk received
50 this.once("error", reject); // Reject on error
51 this.once("close", () => reject(new Error("Connection closed"))); // Reject if the connection is closed
52 });
53 }
54
55 async startProcessing(): Promise<void> {
56 try {
57 const firstData = await this.waitForFirstData();
58 logger.info("tcp fetch started");
59 } catch (err) {
60 console.error("Failed to start processing:", (err as Error).message);
61 throw err;
62 }
63 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected