【问题标题】:How can i reconnect websocket connection when it is closed using angular2-websocket in Angular2 Application?在 Angular2 应用程序中使用 angular2-websocket 关闭 websocket 连接时,如何重新连接它?
【发布时间】:2017-01-24 10:31:56
【问题描述】:

我是 angular2 框架的新手。我使用 angular2-websocket 插件在我的项目中使用 websockets。我的情况是,如果一旦套接字连接关闭,则在服务器端打开套接字时它不会重新连接。我怎么能用angular2-websocket来做到这一点?我的套接字打开后如何重新连接??

这是我的 app.component.ts 代码 sn-p

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");

      }
    );
}

}
}

请帮我解决这个问题。

【问题讨论】:

    标签: javascript angular websocket


    【解决方案1】:

    尝试使用 WebSocketConfig:

    const webSocketConfig = { reconnectIfNotNormalClose: true } as WebSocketConfig;
    
    this.ws                  = new $WebSocket(url, null, webSocketConfig);
    

    【讨论】:

      猜你喜欢
      • 2012-11-27
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      相关资源
      最近更新 更多