我使用此rxjs-websocket library来处理我的应用程序中的websockets。当我使用angular-cli构建站点并在浏览器中打开时,从服务器收到消息时出现错误:
错误错误 在WebSocket.socket.onclose [as __zone_symbol__ON_PROPERTYclose]
但是当我刷新浏览器时,我没有收到错误,消息传递到UI。奇怪的。似乎浏览器正在关闭连接,刷新会触发重新连接(使用chrome)。它可能是我的代码的问题:
活message.service.ts
import { Injectable } from '@angular/core'
import { QueueingSubject } from 'queueing-subject'
import { Observable } from 'rxjs/Observable'
import websocketConnect from 'rxjs-websockets'
import 'rxjs/add/operator/share'
@Injectable()
export class LiveMessageService {
private inputStream: QueueingSubject<any>
public messages: Observable<any>
public connect() {
this.messages = websocketConnect(
'ws://www.dev.com:8080',
this.inputStream = new QueueingSubject<string>()
).messages.share()
}
public send(message: string):void {
this.inputStream.next(message)
}
}
MessageComponent.ts
import { Component, OnInit } from '@angular/core';
import { Subscription } from 'rxjs/Subscription'
import { JwtService, LiveMessageService } from '../services';
export class MessageComponent implements OnInit {
constructor(private liveMessageService: LiveMessageService,private jwtService: JwtService,) { }
public notifications;
public unreadNotifications: boolean;
public playerDetails;
public messageSubscribe: string = "SUBSCRIBE player.";
ngOnInit() {
if (this.jwtService.getPlayer()) {
this.playerDetails = (JSON.parse(this.jwtService.getPlayer()));
this.liveMessageService.connect();
this.liveMessageService.messages.subscribe((message) => {
this.notifications.push(message);
this.unreadNotifications = this.notifications
.filter(notification => notification.read == false).length;
})
// example message 'SUBSCRIBE player.10'
this.liveMessageService.send(this.messageSubscribe
+ this.playerDetails.id)
}
}
}
有人在那里看到任何问题吗?感谢
答案 0 :(得分:0)
这可能是由于浏览器设置导致服务器自行重启