相关疑难解决方法(0)

重新连接Angular和rxjs中的websocket?

我有一个基于ngrx/store(v2.2.2)和rxjs(v5.1.0)的应用程序,它使用observable监听Web套接字的传入数据.当我启动应用程序时,我可以完美地接收传入的数据.

但是过了一段时间(更新很少发生)连接似乎丢失了,我不再收到传入的数据.我的代码:

服务

import { Injectable, OnInit } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable()
export class MemberService implements OnInit {

  private websocket: any;
  private destination: string = "wss://notessensei.mybluemix.net/ws/time";

  constructor() { }

  ngOnInit() { }

  listenToTheSocket(): Observable<any> {

    this.websocket = new WebSocket(this.destination);

    this.websocket.onopen = () => {
      console.log("WebService Connected to " + this.destination);
    }

    return Observable.create(observer => {
      this.websocket.onmessage = (evt) => {
        observer.next(evt);
      };
    })
      .map(res => res.data)
      .share();
  }
}
Run Code Online (Sandbox Code Playgroud)

订户

  export class AppComponent implements OnInit …
Run Code Online (Sandbox Code Playgroud)

websocket rxjs typescript ngrx angular

12
推荐指数
2
解决办法
1万
查看次数

标签 统计

angular ×1

ngrx ×1

rxjs ×1

typescript ×1

websocket ×1