Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 9b0b114

Browse files
Introduce connectToPort method to iOSEmulatorServices
Introduce new method - connectToPort to iOSEmulatorServices. This method will try to connect to specified port on users machine (typically this port will be used by NativeScript applications runnin on iOS Simulator) for a specified amount of time. In case it succeeds, the socket of the connection is returned. In case it fails, undefined is returned.
1 parent 7b6baed commit 9b0b114

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

definitions/mobile.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,34 @@ declare module Mobile {
580580
iOSSimPath: string;
581581
}
582582

583+
/**
584+
* Describes the information when trying to connect to port.
585+
*/
586+
interface IConnectToPortData {
587+
/**
588+
* The port to connect.
589+
* @type {number}
590+
*/
591+
port: number;
592+
593+
/**
594+
* Timeout in milliseconds.
595+
* @type {number}
596+
*/
597+
timeout?: number;
598+
}
599+
583600
interface IiOSSimulatorService extends IEmulatorPlatformServices {
584601
postDarwinNotification(notification: string): Promise<void>;
602+
603+
/**
604+
* Tries to connect to specified port for speciefied amount of time.
605+
* In case it succeeds, a socket is returned.
606+
* In case it fails, undefined is returned.
607+
* @param {IConnectToPortData} connectToPortData Data describing port and timeout to try to connect.
608+
* @returns {net.Socket} Returns instance of net.Socket when connection is successful, otherwise undefined is returned.
609+
*/
610+
connectToPort(connectToPortData: IConnectToPortData): Promise<any>;
585611
}
586612

587613
interface IEmulatorSettingsService {

mobile/ios/simulator/ios-emulator-services.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import * as net from "net";
2+
import { connectEventuallyUntilTimeout } from "../../../helpers";
3+
14
class IosEmulatorServices implements Mobile.IiOSSimulatorService {
5+
private static DEFAULT_TIMEOUT = 10000;
6+
27
constructor(private $logger: ILogger,
38
private $emulatorSettingsService: Mobile.IEmulatorSettingsService,
49
private $errors: IErrors,
@@ -58,6 +63,15 @@ class IosEmulatorServices implements Mobile.IiOSSimulatorService {
5863
await this.$childProcess.spawnFromEvent(nodeCommandName, iosSimArgs, "close", { stdio: "inherit" });
5964
}
6065

66+
public async connectToPort(data: Mobile.IConnectToPortData): Promise<net.Socket> {
67+
try {
68+
const socket = await connectEventuallyUntilTimeout(() => net.connect(data.port), data.timeout || IosEmulatorServices.DEFAULT_TIMEOUT);
69+
return socket;
70+
} catch (e) {
71+
this.$logger.debug(e);
72+
}
73+
}
74+
6175
private async runApplicationOnEmulatorCore(app: string, emulatorOptions?: Mobile.IEmulatorOptions): Promise<any> {
6276
this.$logger.info("Starting iOS Simulator");
6377
const iosSimPath = this.$iOSSimResolver.iOSSimPath;

0 commit comments

Comments
 (0)