我正在尝试使用websockets设置一个简单的通知系统。
我的websocket服务:
import {Injectable} from '@angular/core';
import {Observable, Observer, Subject} from 'rxjs/Rx'
@Injectable()
export class WebSocketService {
private socket: Subject<MessageEvent>;
public connect(url): Subject<MessageEvent> {
if (!this.socket) {
this.socket = this.create(url);
}
return this.socket;
}
private create(url): Subject<MessageEvent> {
let ws = new WebSocket(url);
let observable = Observable.create(
(obs: Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
}
);
let observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
},
};
return Subject.create(observer, observable);
}
}
通知服务包含以下通知:
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Rx'
import {WebSocketService} from "./websocket.service";
@Injectable()
export class NotificationService {
public messages: Subject<Object>;
constructor(wsService: WebSocketService) {
let notificationUrl = `ws://localhost:4969/Notification`;
this.messages = <Subject<Object>>wsService
.connect(notificationUrl)
.map((response: MessageEvent): Object => {
return JSON.parse(response.data);
});
}
}
订阅服务通知主题的组件:
import {Component, OnInit, ElementRef} from '@angular/core';
import {Notification} from "./notification";
import {NotificationService} from "../../services/notification.service";
@Component({
selector: 'notifier',
templateUrl: 'notifier.component.html',
styleUrls: ['notifier.component.css'],
host: {
'(document:click)': 'onClickedOutside($event)',
},
})
export class NotifierComponent implements OnInit {
notifications: Notification[] =[];
constructor(private elementRef: ElementRef, private notificationService: NotificationService) {
}
ngOnInit() {
this.notificationService.messages.subscribe(notification => {
let not = new Notification(notification.title, notification.notificationTypes, notification.data);
console.log(not);
let index = this.notifications.findIndex(x=>x.title == not.title);
if (index ==-1) {
this.notifications.push(not);
}
else {
this.notifications[index].data.progress=not.data.progress;
}
console.log(this.notifications)
});
}
}
所以我的websocket服务器发送通知,可以唯一地识别它的标题。如果标题已经存在于我的通知数组中,我更新它的进度,否则我推送它。
一切都很好,除了一件事。我的视图不会在每次通知更新时更新,但如果我点击任何地方,点击事件将重新渲染,我将看到进度上升。
我读过一些关于Angular2如何传播数据的文章,从我的阅读中我了解到,当你订阅一个主题时,它的数据发生了变化,它应该触发一个事件来重新渲染,但显然不是这样的。
我错过了什么?
我已经能够在这个问题的帮助下解决我的问题:如何在Angular 2中强制组件的重新渲染?
为进一步参考,这里是我在组件的订阅函数中添加的确切行:
this.applicationRef.tick();