Skip to content

Commit 4ebcb94

Browse files
Fix tns debug ios issues
Since we use the ios-device-lib to connect to the device backend port for debugging, the socket returned from the device can be used only in the ios-device lib. That's why we need to change the logic for creating the Node.js socket in the CLI. The ios-device-lib will expose the device socket on a specific port in the localhost. After we connect to the exposed socket we can continue to work like we used to.
1 parent ec5e7c7 commit 4ebcb94

File tree

5 files changed

+19
-25
lines changed

5 files changed

+19
-25
lines changed

lib/declarations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ interface IAndroidToolsInfoData {
290290

291291
interface ISocketProxyFactory {
292292
createTCPSocketProxy(factory: () => any): any;
293-
createWebSocketProxy(factory: () => any): any;
293+
createWebSocketProxy(factory: () => Promise<any>): any;
294294
}
295295

296296
interface IiOSNotification {

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export class SocketProxyFactory implements ISocketProxyFactory {
6767
}
6868

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

@@ -80,13 +79,12 @@ export class SocketProxyFactory implements ISocketProxyFactory {
8079

8180
let server = new ws.Server(<any>{
8281
port: localPort,
83-
verifyClient: (info: any, callback: Function) => {
82+
verifyClient: async (info: any, callback: Function) => {
8483
this.$logger.info("Frontend client connected.");
85-
socketFactory((_socket: any) => {
86-
this.$logger.info("Backend socket created.");
87-
info.req["__deviceSocket"] = _socket;
88-
callback(true);
89-
});
84+
const _socket = await factory();
85+
this.$logger.info("Backend socket created.");
86+
info.req["__deviceSocket"] = _socket;
87+
callback(true);
9088
}
9189
});
9290
server.on("connection", (webSocket) => {
@@ -102,9 +100,10 @@ export class SocketProxyFactory implements ISocketProxyFactory {
102100

103101
webSocket.on("message", (message, flags) => {
104102
let length = Buffer.byteLength(message, encoding);
105-
let payload = new Buffer(length);
106-
payload.write(message, 0, length, encoding);
107-
deviceSocket.write(payload.toString());
103+
let payload = new Buffer(length + 4);
104+
payload.writeInt32BE(length, 0);
105+
payload.write(message, 4, length, encoding);
106+
deviceSocket.write(payload);
108107
});
109108

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

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
1818
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachAvailable(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE)
1919
];
2020

21-
const observeNotificationPromises = _.map(observeNotificationSockets, s => {
22-
return this.$iOSNotificationService.awaitNotification(deviceIdentifier, +s, timeout);
23-
});
21+
const observeNotificationPromises = _(observeNotificationSockets)
22+
.uniq()
23+
.map(s => {
24+
return this.$iOSNotificationService.awaitNotification(deviceIdentifier, +s, timeout);
25+
})
26+
.value();
2427

2528
// Trigger the notifications update.
2629
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachAvailabilityQuery(projectId));

lib/services/ios-debug-service.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,12 @@ class IOSDebugService implements IDebugService {
174174
}
175175

176176
private async wireDebuggerClient(projectData: IProjectData, device?: Mobile.IiOSDevice): Promise<void> {
177-
const factoryPromise = async () => {
177+
const factory = async () => {
178178
let socket = device ? await device.connectToPort(inspectorBackendPort) : net.connect(inspectorBackendPort);
179179
this._sockets.push(socket);
180180
return socket;
181181
};
182-
183-
const socket = await factoryPromise();
184-
const factory = () => {
185-
process.nextTick(() => {
186-
socket.emit("connect");
187-
});
188-
return socket;
189-
};
190-
182+
191183
if (this.$options.chrome) {
192184
this._socketProxy = this.$socketProxyFactory.createWebSocketProxy(factory);
193185

0 commit comments

Comments
 (0)