Skip to content

Commit 0890acb

Browse files
Add receive message from ios device socket logic
1 parent 3eb893a commit 0890acb

File tree

5 files changed

+29
-29
lines changed

5 files changed

+29
-29
lines changed

lib/declarations.ts

-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ interface IiOSNotification {
306306
interface IiOSNotificationService {
307307
awaitNotification(deviceIdentifier: string, socket: number, timeout: number): Promise<string>;
308308
postNotification(deviceIdentifier: string, notification: string, commandType?: string): Promise<string>;
309-
subscribeForNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<Promise<string>>;
310309
}
311310

312311
interface IiOSSocketRequestExecutor {

lib/device-sockets/ios/socket-proxy-factory.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export class SocketProxyFactory implements ISocketProxyFactory {
6666
return server;
6767
}
6868

69-
public createWebSocketProxy(factory: () => net.Socket): ws.Server {
70-
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
69+
public createWebSocketProxy(factory: () => Promise<net.Socket>): ws.Server {
70+
let socketFactory = async (callback: (_socket: net.Socket) => void) => callback(await factory());
7171
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
7272
let localPort = 8080;
7373

@@ -102,10 +102,9 @@ export class SocketProxyFactory implements ISocketProxyFactory {
102102

103103
webSocket.on("message", (message, flags) => {
104104
let length = Buffer.byteLength(message, encoding);
105-
let payload = new Buffer(length + 4);
106-
payload.writeInt32BE(length, 0);
107-
payload.write(message, 4, length, encoding);
108-
deviceSocket.write(payload);
105+
let payload = new Buffer(length);
106+
payload.write(message, 0, length, encoding);
107+
deviceSocket.write(payload.toString());
109108
});
110109

111110
deviceSocket.on("end", () => {

lib/device-sockets/ios/socket-request-executor.ts

+23-12
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,27 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
44
constructor(private $errors: IErrors,
55
private $iOSNotification: IiOSNotification,
66
private $iOSNotificationService: IiOSNotificationService,
7-
private $logger: ILogger) { }
7+
private $logger: ILogger,
8+
private $projectData: IProjectData,
9+
private $iosDeviceOperations: IIOSDeviceOperations) {
10+
this.$iosDeviceOperations.setShouldDispose(false);
11+
}
812

913
public async executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise<void> {
1014
const deviceIdentifier = device.deviceInfo.identifier;
1115

12-
const observeNotificationPromises = [
13-
await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.getAlreadyConnected(projectId), timeout),
14-
await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), timeout),
15-
await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.getAttachAvailable(projectId), timeout)
16+
const observeNotificationSockets = [
17+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.alreadyConnected, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE),
18+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE),
19+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachAvailable, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE)
1620
];
1721

22+
const observeNotificationPromises = _.map(observeNotificationSockets, s => {
23+
return this.$iOSNotificationService.awaitNotification(deviceIdentifier, +s, timeout);
24+
});
25+
1826
// Trigger the notifications update.
19-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachAvailabilityQuery(projectId), constants.IOS_POST_NOTIFICATION_COMMAND_TYPE);
27+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachAvailabilityQuery);
2028

2129
let receivedNotification: string;
2230
try {
@@ -43,16 +51,18 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
4351

4452
public async executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, projectId: string, shouldBreak?: boolean): Promise<void> {
4553
try {
46-
const appLaunchingPromise = await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.getAppLaunching(projectId), timeout);
47-
await appLaunchingPromise;
54+
const appLaunchingSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.appLaunching, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
55+
await this.$iOSNotificationService.awaitNotification(deviceIdentifier, +appLaunchingSocket, timeout);
56+
4857
if (shouldBreak) {
4958
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getWaitForDebug(projectId), constants.IOS_POST_NOTIFICATION_COMMAND_TYPE);
5059
}
5160

5261
// We need to send the ObserveNotification ReadyForAttach before we post the AttachRequest.
53-
const readyForAttachPromise = await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), readyForAttachTimeout);
62+
const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
63+
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, readyForAttachTimeout);
5464

55-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId), constants.IOS_POST_NOTIFICATION_COMMAND_TYPE);
65+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest);
5666
await readyForAttachPromise;
5767
} catch (e) {
5868
this.$logger.trace("Launch request error:");
@@ -65,8 +75,9 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
6575
try {
6676
// We should create this promise here because we need to send the ObserveNotification on the device
6777
// before we send the PostNotification.
68-
const readyForAttachPromise = await this.$iOSNotificationService.subscribeForNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), timeout);
69-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId));
78+
const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.readyForAttach, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
79+
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, timeout);
80+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.attachRequest);
7081
await readyForAttachPromise;
7182
} catch (e) {
7283
this.$errors.failWithoutHelp(`The application ${projectId} timed out when performing the socket handshake.`);

lib/services/ios-notification-service.ts

-9
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ export class IOSNotificationService implements IiOSNotificationService {
1515
return _.first(notificationResponse[deviceIdentifier]).response;
1616
}
1717

18-
public subscribeForNotification(deviceIdentifier: string, notification: string, timeout: number): Promise<Promise<string>> {
19-
return new Promise((resolve, reject) => {
20-
this.postNotification(deviceIdentifier, notification, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE)
21-
.then((socket) => {
22-
resolve(this.awaitNotification(deviceIdentifier, +socket, timeout));
23-
});
24-
});
25-
}
26-
2718
public async postNotification(deviceIdentifier: string, notification: string, commandType?: string): Promise<string> {
2819
commandType = commandType || constants.IOS_POST_NOTIFICATION_COMMAND_TYPE;
2920
const response = await this.$iosDeviceOperations.postNotification([{ deviceId: deviceIdentifier, commandType: commandType, notificationName: notification }]);

0 commit comments

Comments
 (0)