【发布时间】:2021-10-02 20:09:07
【问题描述】:
我正在尝试与 @stomp/stompjs 建立 websocket 连接,尽管在控制台中连接成功,但数据没有更新,任何关于我做错了什么的想法,我已经在线阅读了所有内容我不明白为什么它不起作用。
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Stomp } from '@stomp/stompjs';
import * as SockJS from 'sockjs-client';
import { MessagesService } from 'src/app/services/messages.service';
@Component({
selector: 'app-classtwo',
templateUrl: './classtwo.component.html',
styleUrls: ['./classtwo.component.scss']
})
export class ClasstwoComponent implements OnInit, OnDestroy {
public mail_list:any [] = []
private stompClient = Stomp.over(new SockJS('http://localhost:8080/mailbot-websocket'));
constructor(private service:MessagesService, private router: Router) { }
ngOnInit(): void {
this._loadEmailsFromRestApi()
this._loadEmailsFromWebSocket()
}
private _loadEmailsFromWebSocket() {
let that = this;
this.stompClient.connect({}, () =>{
that.stompClient.subscribe('/topic/processed/TYPE_II', this.callback)
})
}
private _loadEmailsFromRestApi() {
this.service.getPendingMailsByCategory('TYPE_II').subscribe( res => {
this.mail_list = res
})
}
ngOnDestroy(): void {
if (this.stompClient != null) {
this.stompClient.disconnect(()=> {
console.log("DISCONECTED");
});
}
}
goToMailDescription(category:string, id:string) {
this.router.navigate(['/mailDetail/' + category + '/' + id]);
}
callback = (message:any) => {
if(message.body)
this.mail_list = JSON.parse(message.body)
}
}
注意:似乎在重新加载页面后它会收到消息,但只有在您继续运行它时它是相同的,直到再次重新加载。
【问题讨论】:
标签: angular typescript websocket stomp