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

Introduce connectToPort method to iOSEmulatorServices #1041

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions definitions/mobile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;

/**
* 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<any>;
}

interface IEmulatorSettingsService {
Expand Down
14 changes: 14 additions & 0 deletions mobile/ios/simulator/ios-emulator-services.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<net.Socket> {
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<any> {
this.$logger.info("Starting iOS Simulator");
const iosSimPath = this.$iOSSimResolver.iOSSimPath;
Expand Down