如何使用ngfor将websocket数据变为可观察和显示

时间:2019-06-12 10:29:41

标签: angular typescript websocket observable ngfor

我从binance websocket获取数据,并尝试使用ngfor在表上显示其属性,但在控制台上收到“无法读取属性'dispose of null'”错误。我需要更改什么?

我尝试不使用ngfor只获取一行数据,并且成功了。但是我需要在桌子上获得多个值。

这是我的套接字服务:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as socketIo from 'socket.io-client';
import * as Rx from 'rxjs/Rx';

@Injectable({
  providedIn: 'root'
})
export class SocketService {
  constructor(private http: HttpClient) { }

  private subject: Rx.Subject<MessageEvent>;

  public connect(url): Rx.Subject<MessageEvent> {
    if (!this.subject) {
      this.subject = this.create(url);
      console.log('Successfully connected: ' + url);
    }
    return this.subject;
  }

  private create(url): Rx.Subject<MessageEvent> {
    let ws = new WebSocket(url);

    let observable = Rx.Observable.create((obs: Rx.Observer<MessageEvent>) => {
      ws.onmessage = obs.next.bind(obs);
      ws.onerror = obs.error.bind(obs);
      ws.onclose = obs.complete.bind(obs);
      return ws.close.bind(ws);
    });
    let observer = {
      next: (data: Object) => {
        if (ws.readyState === WebSocket.OPEN) {
          ws.send(JSON.stringify(data));
        }
      }
    };
    return Rx.Subject.create(observer, observable);
  }
}

这是我的邮件服务

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { SocketService } from './socket.service';

const CHAT_URL = 'wss://stream.binance.com:9443/ws/ethbtc@ticker';

export interface Message {
  sym: string;
  prch: string;
  prchp: string;
  lastp: string;


}


@Injectable({
  providedIn: 'root'
})


export class MsgService {
  public messages: Subject<Message>;

constructor(wsService: SocketService) {  this.messages = wsService.connect(CHAT_URL).map(
  (response: MessageEvent): Message => {
    const data = JSON.parse(response.data);
    return {
      sym: data.s,
      lastp: data.c,
      prch: data.p,
      prchp: data.P
    };
    }
) as Subject<Message>;

}
sendMsg(msg) {
  this.messages.next(msg);
}

}

这是我的组成部分

import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs';
import { ApiService } from '../api.service';
import { Router } from '@angular/router';
import { MsgService, Message } from '../msg.service';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {

  public values: Message;

  constructor(private apiservice: ApiService,private router: Router,private msgService: MsgService) { }

  ngOnInit() {


    this.msgService.messages.subscribe(data => this.values = data);

  }

onSelect(value){
this.router.navigate(['/detailed', value.symbol]);
}

}

是html

<div class="container">


<h1 style="margin-left: 40%">
  Currencies
</h1>

<table class="table table-bordered table-info ">
  <thead>
    <tr>
      <th>Currency</th>
      <th>Last Price</th>
      <th>24h Volume</th>
      <th>24h Pct Change</th>

    </tr>
  </thead>
  <tbody>
    <tr (click)="onSelect(value)" *ngFor="let value of values | async">
      <td><a [routerLink]="['/detailed']"> {{value.sym}} </a></td>
      <td>{{value.prch}}</td>
      <td>{{value.prchp}}</td>
      <td>{{value.lastp}}</td>
    </tr>

  </tbody>
</table>

</div>

我希望让html表充满来自websocket的数据。相反,我是在控制台上得到的:

 ERROR TypeError: Cannot read property 'dispose' of null
    at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe._dispose (common.js:4937)
    at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe.transform (common.js:4912)
    at Object.eval [as updateDirectives] (MainComponent.html:19)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)
    at checkAndUpdateView (core.js:23307)
    at callViewAction (core.js:23548)
    at execComponentViewsAction (core.js:23490)
    at checkAndUpdateView (core.js:23313)
    at callViewAction (core.js:23548)
    at execEmbeddedViewsAction (core.js:23511)

0 个答案:

没有答案