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

Commit f978b57

Browse files
Override net.Socket write method
1 parent a82b75c commit f978b57

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Socket } from "net";
2+
3+
export class IOSDeviceSocket extends Socket {
4+
private deviceIdentifier: string;
5+
private fd: string;
6+
private iosDeviceOperations: IIOSDeviceOperations;
7+
8+
public initialize(fd: string, deviceIdentifier: string, iosDeviceOperatios: IIOSDeviceOperations): void {
9+
this.fd = fd;
10+
this.deviceIdentifier = deviceIdentifier;
11+
this.iosDeviceOperations = iosDeviceOperatios;
12+
}
13+
14+
public async write(message: string): Promise<number> {
15+
const response = await this.iosDeviceOperations.sendMessageToSocket([{ socket: +this.fd, message: message, deviceId: this.deviceIdentifier }]);
16+
17+
return +_.first(response[this.deviceIdentifier]).response;
18+
}
19+
}

mobile/ios/device/ios-device.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as applicationManagerPath from "./ios-application-manager";
22
import * as fileSystemPath from "./ios-device-file-system";
33
import * as constants from "../../../constants";
44
import * as net from "net";
5+
import { IOSDeviceSocket } from "./ios-device-socket";
56

67
export class IOSDevice implements Mobile.IiOSDevice {
78
public applicationManager: Mobile.IDeviceApplicationManager;
@@ -60,7 +61,11 @@ export class IOSDevice implements Mobile.IiOSDevice {
6061
const deviceId = this.deviceInfo.identifier;
6162
const deviceResponse = (await this.$iosDeviceOperations.connectToPort([{ deviceId: deviceId, port: port }]))[deviceId];
6263

63-
this._socket = new net.Socket({ fd: deviceResponse[0].response });
64+
// HACK to override the write method of net.Socket
65+
const fd = deviceResponse[0].response;
66+
const socket = new IOSDeviceSocket({ fd: fd });
67+
socket.initialize(fd, deviceId, this.$iosDeviceOperations);
68+
this._socket = <any>socket;
6469

6570
this.$processService.attachToProcessExitSignals(this, this.destroySocket);
6671
return this._socket;

0 commit comments

Comments
 (0)