NathanaelA / nativescript-websockets

Websockets for NativeScript
83 stars 43 forks source link

Plugin does not establish connection unless a call to Open is made. #50

Closed DeepakArora76 closed 6 years ago

DeepakArora76 commented 6 years ago

The behaviour can be seen on Android and iOS. With below code, I am unable see any logs, and that means the activity to open connection remain dormant / passive. If I add this.websocket.open() after the line new WS(address, {timeout: 6000});, I see that all the registered callbacks are triggered.

   const address: string = `ws://${hostIpAddress}:${GuestModeComponent.PORT}`;
   this.websocket =  new WS(address, {timeout: 6000});
   this.websocket.on("open", socket => {
      this.ngZone.run(() => this.onConnectionOpened(socket));
   });

   this.websocket.on("close", (socket, code, reason) => {
      console.log("Close");
      this.ngZone.run(() => this.onConnectionClosed(socket, code, reason));
   });

   this.websocket.on("error", (socket, error) => {
      console.log("ERROR");
      this.ngZone.run(() => this.onConnectionError(socket, error));
   });

   this.websocket.on("message", (socket, message) => {
      this.ngZone.run(() => this.onMessageReceived(socket, message));
   });

Additional details:

DeepakArora76 commented 6 years ago

Another issue, is that the timeout for iOS is not respected. In the above sample, if I have a connection timeout of 6 seconds, then on iOS it takes more than a min + to see an errror.

DeepakArora76 commented 6 years ago

You even documented information is wrong for iOS, specific to timeout! I do not find any reference to "Defaults to 60,0000ms on IOS" in the source code. Appreciate a link to that file.

Also, the timeout is in second: See below link. https://developer.apple.com/documentation/foundation/nsurlrequest/1416292-init?changes=_2

So, if I make a call to new WS(address, {timeout: 6}) on iOS, then it works. It times out after 6 secs.

NathanaelA commented 6 years ago

Well first of all, the websocket will not auto-open if you are using the advanced interface this is documented in the https://github.com/NathanaelA/nativescript-websockets/blob/master/src/README.md#browser-based-websockets section. ONLY Browser based websockets auto-open to be identical to how the websockets on browsers behave. If you are choosing to use the advanced websockets; you control when to open it.

Second on the timeout; https://developer.apple.com/documentation/foundation/nsmutableurlrequest/1414063-timeoutinterval?changes=_2 ( 60 seconds = 60,000ms )

Third you are correct, timeouts are in seconds on iOS; I probably should make a note of that in the docs... I need to double check what they are on Android and I'll make sure to keep them the same. One of them might need to be converted to keep them in the same type (i.e. seconds or ms)

DeepakArora76 commented 6 years ago

Thanks 👍