Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function watchMessage() in Angular is never called? #73

Open
manhhungptit opened this issue Aug 12, 2020 · 2 comments
Open

Function watchMessage() in Angular is never called? #73

manhhungptit opened this issue Aug 12, 2020 · 2 comments

Comments

@manhhungptit
Copy link

Hi, I'm using your plugin in a Ionic app build with Angular and Capacitor build.

In the app, I create a WebSocket Server instance, then using watchMessage().subscribe to listen to any message that come. Here is my service class

import { WebSocketServer } from '@ionic-native/web-socket-server/ngx';

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class WebsocketServerService {

  constructor(private wsserver: WebSocketServer) {}

  async init() {
    if ((window as any)?.cordova) {
      console.log('Websocket init');
      this.wsserver.start(8888, {}).subscribe({
        next: server => console.log(server, `Websocket Server: Listening on ${server.addr}:${server.port}`),
        error: error => console.log(`Websocket Server: Unexpected error`, error),
      });

      console.log('Websocket Server: start handler');

      // --> this is never called
      this.wsserver.watchMessage().subscribe(msg => {
        console.log('Websocket Server: got msg: ', msg);
      });
    } else {
      console.log('Websocket Server: cant start, cordova not available');
    }
  }

  public stop() {
    return this.wsserver.stop();
  }

  public send() {}

}

The connection is established and connected well with a websocket client, but after I sent a message "Hello" from websocket client, the subscribe callback function was not called, even though the log on the device show this:

WebSocketServer: Server should accept request: http://192.168.31.69:8888/
WebSocketServer: WebSocket did open
WebSocketServer: Websocket did receive message: Hello

Thank you.

@souravsasmal
Copy link

Hi there,

Did you resolve it? I am also having the same problem. If you have found any solution, please let me know.

Thanks.

@chetan-prime
Copy link

chetan-prime commented Sep 20, 2021

The ionic wrapper is broken, at least with the current version of the plugin.
The only way I could use in angular is drop the watch* subscribe functions and instead use the javascript like API via wsServer.

let wsOpts = {
  'onOpen': (conn) => {
    // your code
  },
  'onMessage': (conn, msg) => {
    // your code
  }, ...
};

this.wsServer.open(<WebSocketOptions> wsOpts);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants