Angular: RxStomp Watch, Configure, Activate Method
In Stomp abbiamo usato questi metodi:
initUserEvents() {
this.stompService.startConnect().then(() => {
this.stompService.done('init');
this.stompService.subsribe('/channel/login', res => {
if (res.username !== this.username) {
this.newConnectedAccounts.push(res.username);
in questo momento sto lavorando con RxStomp e non riesco a capire il modo giusto per utilizzare questi metodi di "sostituzione":
initUserEvents() {
this.stompService.configure();
this.stompService.activate(){
this.stompService.watch('/channel/login', res => {
if (res.username !== this.username) {
this.newConnectedAccounts.push(res.username);
L'errore che ricevo è: TS2345: l'argomento di tipo "(res: any) => void" non è assegnabile al parametro di tipo "StompHeaders". La firma dell'indice è mancante nel tipo '(res: any) => void'.
Risposte
Dai un'occhiata alla documentazione di RxStomp. Dice:
La differenza fondamentale è che espone le operazioni come osservabili RxJS. Ad esempio, quando un endpoint STOMP viene sottoscritto, restituisce un Observable che trasmetterà tutti i messaggi ricevuti.
Ad eccezione di beforeConnect, la funzionalità relativa a tutti i callback nel client @ stomp / stompjs è esposta come Observables / Subject / BehaviorSubjects.
In altre parole, non si passa una richiamata al watchmetodo. Restituisce un messaggio Observableche puoi utilizzare per ricevere i messaggi.
L'API RxStomp si aspetta che tu passi le intestazioni al watchmetodo, non un callback. Ecco perché ti sta dicendo specificamente:
TS2345: Argument of type '(res: any) => void' is not assignable to parameter of type 'StompHeaders'.