becvert / cordova-plugin-websocket-server

Cordova WebSocket Server Plugin
MIT License
84 stars 35 forks source link

This plugin & Ionic 3 #43

Closed OliverTr54 closed 6 years ago

OliverTr54 commented 6 years ago

Hey folks,

I really tried everything, but I can't make it to work - and I really really need this plugin.

I tried it with window.cordova.plugins, cordova.plugins and also by declaring

declare var wsserver: any;

...but when I start it on my Android device for example - no error at all and the app keep runing, but I get no feedback from the plugin itself.

What can I do? I really have to have this plugin!!!

Thanks in advance, Oliver

OliverTr54 commented 6 years ago

My code currently - I was also trying to change the port - but no reaction at all:


    openWS() {
        this.information.push("Is platform ready?");
        this.platform.ready().then(() => {
            this.information.push("Platform ready.");
            var wsserver = window.cordova.plugins.wsserver;
             wsserver.start(443, {
                'onFailure' :  function(addr, port, reason) {
                    console.log('Stopped listening on %s:%d. Reason: %s', addr, port, reason);
                    this.information.push("Stopped listening. Reason: "+reason);
                },
                'onOpen' : function(conn) {
                    console.log('A user connected from %s', conn.remoteAddr);
                    this.information.push("User connected from: "+conn.remoteAddr);
                },
                'onMessage' : function(conn, msg) {
                    console.log(conn, msg);
                    this.information.push(msg);
                },
                'onClose' : function(conn, code, reason, wasClean) {
                    console.log('A user disconnected from %s', conn.remoteAddr);
                    this.information.push("Connection has been close: "+conn.remoteAddr);
                },
                // Other options
                'origins' : [ 'file://' ], // validates the 'Origin' HTTP Header.
                'protocols' : [ 'my-protocol-v1', 'my-protocol-v2' ], // validates the 'Sec-WebSocket-Protocol' HTTP Header.
                'tcpNoDelay' : true // disables Nagle's algorithm.              
             }
             , function onStart(addr, port) {
                console.log('Listening on %s:%d', addr, port);
                this.information.push("Started listening: "+addr+" ("+port+")");
             }
             , function onDidNotStart(reason) {
                console.log('Did not start. Reason: %s', reason);
                this.information.push("Did not start. Reason: "+reason);
             }
            );
        });
    }
becvert commented 6 years ago

I do not use nor know how ionic works.

2 things however:

Good luck

OliverTr54 commented 6 years ago

It works - I had to use chrome remote debugging, because it was throwing an exception that "this.information" is undefined - but that's some other issue that is to be solved differently. Thanks again anyway - it's a great plugin and has a huge impact on my project or what I'm planing to do.

Oliver

OliverTr54 commented 6 years ago

Here's an example how I use it with Ionic 3, cause I have seen some issues about using your module...

import { Component } from '@angular/core';
import { NavController, Platform } from 'ionic-angular';
import ...

declare var cordova;

@Component({
    selector: 'page-test',
    templateUrl: 'test.html'
})

export class TestPage {

    constructor(public navCtrl: NavController, public platform: Platform) {
    }

    ...

    startSocketServer() {
        this.platform.ready().then(() => {          
             var wsserver = cordova.plugins.wsserver;
             wsserver.start(0, {
                'onFailure' :  function(addr, port, reason) {
                    console.log('Stopped listening on %s:%d. Reason: %s', addr, port, reason);
                },
                'onOpen' : function(conn) {
                    console.log('A user connected from %s', conn.remoteAddr);
                },
                'onMessage' : function(conn, msg) {
                    console.log(conn, msg);
                },
                'onClose' : function(conn, code, reason, wasClean) {
                    console.log('A user disconnected from %s', conn.remoteAddr);
                }
             }
             , function onStart(addr, port) {
                console.log('Listening on %s:%d', addr, port);
             }
             , function onDidNotStart(reason) {
                console.log('Did not start. Reason: %s', reason);
             }
            );
        });
    }

    ...

}

The port option "0" will just choose some random port that is not in use.

Oliver