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

Commit 6fd98a6

Browse files
Add receive message from ios device socket logic
1 parent 741a91d commit 6fd98a6

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

definitions/mobile.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ interface IIOSDeviceOperations extends IDisposable {
720720

721721
sendMessageToSocket(sendMessageToSocketArray: IOSDeviceLib.ISendMessageToSocketData[], errorHandler?: DeviceOperationErrorHandler): Promise<IOSDeviceResponse>;
722722

723+
readMessageFromSocket(readMessageFromSocketArray: IOSDeviceLib.IReceiveMessagesFromSocketData[]): void;
724+
723725
connectToPort(connectToPortArray: IOSDeviceLib.IConnectToPortData[], errorHandler?: DeviceOperationErrorHandler): Promise<IOSDeviceResponse>;
724726

725727
setShouldDispose(shouldDispose: boolean): void;

helpers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,22 @@ export function connectEventually(factory: () => net.Socket, handler: (_socket:
352352
tryConnect();
353353
}
354354

355+
export async function connectEventuallyAsync(factory: () => Promise<net.Socket>, handler: (_socket: net.Socket) => void): Promise<void> {
356+
async function tryConnect() {
357+
let tryConnectAfterTimeout = setTimeout.bind(undefined, tryConnect, 1000);
358+
359+
let socket = await factory();
360+
socket.on("connect", () => {
361+
socket.removeListener("error", tryConnectAfterTimeout);
362+
handler(socket);
363+
});
364+
socket.on("error", tryConnectAfterTimeout);
365+
}
366+
367+
await tryConnect();
368+
}
369+
370+
355371
export async function connectEventuallyUntilTimeout(factory: () => net.Socket, timeout: number): Promise<net.Socket> {
356372
return new Promise<net.Socket>((resolve, reject) => {
357373
let lastKnownError: Error;

mobile/ios/device/ios-device-operations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ export class IOSDeviceOperations implements IIOSDeviceOperations, IDisposable {
169169
return this.getMultipleResults<IOSDeviceLib.IDeviceResponse>(() => this.deviceLib.sendMessageToSocket(sendMessageToSocketArray), errorHandler);
170170
}
171171

172+
public readMessageFromSocket(readMessageFromSocketArray: IOSDeviceLib.IReceiveMessagesFromSocketData[]): void {
173+
this.deviceLib.readMessagesFromSocket(readMessageFromSocketArray);
174+
}
175+
172176
public async connectToPort(connectToPortArray: IOSDeviceLib.IConnectToPortData[], errorHandler?: DeviceOperationErrorHandler): Promise<IOSDeviceResponse> {
173177
this.assertIsInitialized();
174178

mobile/ios/device/ios-device-socket.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ import { Socket } from "net";
22

33
export class IOSDeviceSocket extends Socket {
44
private deviceIdentifier: string;
5-
private fd: string;
5+
private fd: number;
66
private iosDeviceOperations: IIOSDeviceOperations;
77

88
public initialize(fd: string, deviceIdentifier: string, iosDeviceOperatios: IIOSDeviceOperations): void {
9-
this.fd = fd;
9+
this.fd = +fd;
1010
this.deviceIdentifier = deviceIdentifier;
1111
this.iosDeviceOperations = iosDeviceOperatios;
12+
13+
const callback = (socketInfo: IOSDeviceLib.ISocketMessage) => {
14+
if (+socketInfo.socket === this.fd) {
15+
this.emit("message", socketInfo.message);
16+
this.emit("data", socketInfo.message);
17+
}
18+
};
19+
this.iosDeviceOperations.readMessageFromSocket([{ socket: this.fd, deviceId: this.deviceIdentifier, context: this, callback: callback }]);
1220
}
1321

1422
public async write(message: string): Promise<number> {
15-
const response = await this.iosDeviceOperations.sendMessageToSocket([{ socket: +this.fd, message: message, deviceId: this.deviceIdentifier }]);
23+
const response = await this.iosDeviceOperations.sendMessageToSocket([{ socket: this.fd, message: message, deviceId: this.deviceIdentifier }]);
1624

1725
return +_.first(response[this.deviceIdentifier]).response;
1826
}

0 commit comments

Comments
 (0)