cordova-plugin-websocket-server
cordova-plugin-websocket-server copied to clipboard
Function watchMessage() in Angular is never called?
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.
Hi there,
Did you resolve it? I am also having the same problem. If you have found any solution, please let me know.
Thanks.
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);