Skip to content

Commit d16cc33

Browse files
Merge pull request #1722 from NativeScript/vladimirov/fix-ios-socket-issues
Fix iOS socket issues
2 parents 9aedb31 + b2ebe84 commit d16cc33

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/services/livesync/ios-livesync-service.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ class IOSLiveSyncService extends liveSyncServiceBaseLib.LiveSyncServiceBase<Mobi
3939
if (!this.socket) {
4040
helpers.connectEventually(() => net.connect(IOSLiveSyncService.BACKEND_PORT), (socket: net.Socket) => {
4141
this.socket = socket;
42-
if(this.$options.watch) {
43-
this.attachProcessExitHandlers();
44-
}
42+
this.attachEventHandlersIfNecessary();
4543
this.sendPageReloadMessage();
4644
});
4745
} else {
@@ -52,12 +50,27 @@ class IOSLiveSyncService extends liveSyncServiceBaseLib.LiveSyncServiceBase<Mobi
5250
if(!this.socket) {
5351
this.$iOSSocketRequestExecutor.executeAttachRequest(this.device, timeout).wait();
5452
this.socket = this.device.connectToPort(IOSLiveSyncService.BACKEND_PORT);
53+
this.attachEventHandlersIfNecessary();
5554
}
5655
this.sendPageReloadMessage();
5756
}
5857
}).future<void>()();
5958
}
6059

60+
private attachEventHandlersIfNecessary(): void {
61+
if(this.$options.watch) {
62+
this.attachProcessExitHandlers();
63+
this.attachSocketCloseEvent();
64+
}
65+
}
66+
67+
private attachSocketCloseEvent(): void {
68+
this.socket.on("close", (hadError: boolean) => {
69+
this.$logger.trace(`Socket closed, hadError is ${hadError}.`);
70+
this.socket = null;
71+
});
72+
}
73+
6174
private sendPageReloadMessage(): void {
6275
try {
6376
this.sendPageReloadMessageCore();
@@ -101,8 +114,10 @@ class IOSLiveSyncService extends liveSyncServiceBaseLib.LiveSyncServiceBase<Mobi
101114
}
102115

103116
private destroySocket(): void {
104-
this.socket.destroy();
105-
this.socket = null;
117+
if(this.socket) {
118+
this.socket.destroy();
119+
this.socket = null;
120+
}
106121
}
107122
}
108123
$injector.register("iosLiveSyncServiceLocator", {factory: IOSLiveSyncService});

0 commit comments

Comments
 (0)