我是angular2框架的新手。我使用angular2-websocket插件在我的项目中使用websockets。我的方案是,如果一旦套接字连接关闭,它就不会在服务器端打开套接字时重新连接。我怎么能用angular2-websocket做到这一点?如何在打开套接字时重新连接到套接字?
这是我的app.component.ts代码段
import { Component, OnInit } from '@angular/core';
import { $WebSocket, WebSocketSendMode } from 'angular2-websocket/angular2-websocket';
import { WebsocketService } from '../../../services/websocket.service';
@Component({
selector: 'app-init',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private websocket:WebsocketService) { }
ngOnInit() {
this.fnGraphInit("ws://10.1.40.117:5555");
}
fnGraphInit(url){
let ws = new $WebSocket(url);
ws.getDataStream().subscribe(
(msg) => {
console.log(msg.data);
(msg) => {
console.log("error", msg);
},
() => {
console.log("complete");
}
);
}
}
}
请帮我解决这个问题。
答案 0 :(得分:3)
尝试使用WebSocketConfig:
const webSocketConfig = { reconnectIfNotNormalClose: true } as WebSocketConfig;
this.ws = new $WebSocket(url, null, webSocketConfig);