diff --git a/definitions/mobile.d.ts b/definitions/mobile.d.ts index 92076be7..40e0103f 100644 --- a/definitions/mobile.d.ts +++ b/definitions/mobile.d.ts @@ -580,8 +580,34 @@ declare module Mobile { iOSSimPath: string; } + /** + * Describes the information when trying to connect to port. + */ + interface IConnectToPortData { + /** + * The port to connect. + * @type {number} + */ + port: number; + + /** + * Timeout in milliseconds. + * @type {number} + */ + timeout?: number; + } + interface IiOSSimulatorService extends IEmulatorPlatformServices { postDarwinNotification(notification: string): Promise; + + /** + * Tries to connect to specified port for speciefied amount of time. + * In case it succeeds, a socket is returned. + * In case it fails, undefined is returned. + * @param {IConnectToPortData} connectToPortData Data describing port and timeout to try to connect. + * @returns {net.Socket} Returns instance of net.Socket when connection is successful, otherwise undefined is returned. + */ + connectToPort(connectToPortData: IConnectToPortData): Promise; } interface IEmulatorSettingsService { diff --git a/mobile/ios/simulator/ios-emulator-services.ts b/mobile/ios/simulator/ios-emulator-services.ts index c1539246..49ee31a8 100644 --- a/mobile/ios/simulator/ios-emulator-services.ts +++ b/mobile/ios/simulator/ios-emulator-services.ts @@ -1,4 +1,9 @@ +import * as net from "net"; +import { connectEventuallyUntilTimeout } from "../../../helpers"; + class IosEmulatorServices implements Mobile.IiOSSimulatorService { + private static DEFAULT_TIMEOUT = 10000; + constructor(private $logger: ILogger, private $emulatorSettingsService: Mobile.IEmulatorSettingsService, private $errors: IErrors, @@ -58,6 +63,15 @@ class IosEmulatorServices implements Mobile.IiOSSimulatorService { await this.$childProcess.spawnFromEvent(nodeCommandName, iosSimArgs, "close", { stdio: "inherit" }); } + public async connectToPort(data: Mobile.IConnectToPortData): Promise { + try { + const socket = await connectEventuallyUntilTimeout(() => net.connect(data.port), data.timeout || IosEmulatorServices.DEFAULT_TIMEOUT); + return socket; + } catch (e) { + this.$logger.debug(e); + } + } + private async runApplicationOnEmulatorCore(app: string, emulatorOptions?: Mobile.IEmulatorOptions): Promise { this.$logger.info("Starting iOS Simulator"); const iosSimPath = this.$iOSSimResolver.iOSSimPath;