1

I'm not able to create the "Advanced Interface" of nativescript-websocket in Angular/Typescript, I need it to use a custom sslFactory to trust all certs.

The problem is that nothing happens and I can't see any error messagge when I'm trying to connect to the server. The server is alive and responsive and I can communicate with it using the "Browser based Interface"

constructor(private zone: NgZone,
            private _http: HttpWrapperService) {

    var WS = require('nativescript-websockets');

    this.mySocket = new WS(AppGlobals.WS_HINT_ADDRESS,{
        protocols:[AppGlobals.WS_HINT_PROTOCOL],
        timeout: 6000, allowCellular: true,
        sslSocketFactory: this._http.sslSocketFactory
    });

    this.mySocket.on('open', function(socket) { 
        this.zone.run(() => {
            console.log("---------Hey I'm open");
        });  
    });
    this.mySocket.on('message', function(socket, message) { 
        this.zone.run(() => {
            console.log("---------Got a message", message); 
        });
    });
    this.mySocket.on('close', function(socket, code, reason) { 
        this.zone.run(() => {
            console.log("---------Socket was closed because: ", reason, " code: ", code); 
        });
    });
    this.mySocket.on('error', function(socket, error) { 
        this.zone.run(() => {
            console.log("---------Socket had an error", error);
        });
    });

}
Massimo Magliani
  • 649
  • 2
  • 7
  • 17
  • Are you opening the websocket with `this.mySocket.open()`? https://www.npmjs.com/package/nativescript-websockets#open – Eduardo Speroni May 07 '19 at 14:41
  • Hi , thanks for your answer. Is the .open() method necessary? When I use it the application crashes with this message : System.err: com.tns.NativeScriptException: System.err: Calling js method onOpen failed System.err: TypeError: Cannot read property 'run' of undefined System.err: File: "file:///data/data/org.nativescript.NSSideKickTest/files/app/app/home/home.component.js, line: 22, column: 22 ....... – Massimo Magliani May 07 '19 at 14:54
  • "Cannot read property 'run' of undefined" - Are you sure you are not trying to call any method named run (which seems to be undefined at that moment) in your component? – Manoj May 07 '19 at 16:49
  • @Manoj : Yes, you are right. When I use this.zone.run() inside the callbacks it raises an error. To solve the problem I need to use lambda functions instead of classic function. Thanks. – Massimo Magliani May 08 '19 at 12:56
  • 1
    Only arrow functions will retain the reference to this of parent scope, since you are using normal function, this will be different from the component. Either store reference of this in a variable and access it Or simply change it to arrow function like `this.mySocket.on('error',(socket, error) => { ... }` – Manoj May 08 '19 at 13:01

1 Answers1

0

I faced a similar issue where the OpenSSL server was alive and responsive and even the handshake was completed successfully but the OPEN event would not trigger. Seems like OpenSSL server does not support websocket. Try using a secured server(https) developed using nodejs. It worked for me.