Jos*_*ose 19 javascript websocket angular
我正在使用这个rxjs-websocket库来处理我的应用程序中的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)
}
}
Run Code Online (Sandbox Code Playgroud)
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人在那里看到任何问题?谢谢
| 归档时间: |
|
| 查看次数: |
740 次 |
| 最近记录: |