Я новичок в RxJ и пытаюсь настроить простой клиентский веб-сокет.Я следовал этому учебнику , но я получаю следующую ошибку в ticker.service.ts:
Собственность 'map' не существует для типа Subject '
websocket.service.ts
import {Injectable} from '@angular/core';
import {Observable, Observer, Subject} from 'rxjs';
import {LiveTrade} from '../models/models';
const CHAT_URL = 'ws://localhost:8888';
@Injectable({
providedIn: 'root'
})
export class WebsocketService {
public tickerStreamObservableSubject: Subject<LiveTrade>;
private msgEventStreamObservableSubject: Subject<MessageEvent>;
public getObservableTicker(url): Subject<MessageEvent> {
if (!this.msgEventStreamObservableSubject) {
this.msgEventStreamObservableSubject = this.createObservable(url);
console.log('Successfully connected: ' + url);
}
return this.msgEventStreamObservableSubject;
}
private createObservable(url): Subject<MessageEvent> {
const ws = new WebSocket(url);
const observable = Observable.create((observer: Observer<MessageEvent>) => {
ws.onmessage = observer.next.bind(observer);
ws.onerror = observer.error.bind(observer);
ws.onclose = observer.complete.bind(observer);
return ws.close.bind(ws);
});
const innerObserverOfSubject: Observer<Object> = {
next: (data: Object) => {
console.log('next called on innerObserverOfSubject')
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
},
error() {
},
complete() {
},
};
return Subject.create(innerObserverOfSubject, observable);
}
constructor() {
}
}
ticker.service.ts
import {Injectable} from '@angular/core';
import {Observable, Subject} from 'rxjs';
import {WebsocketService} from './websocket.service';
import {LiveTrade} from '../models/models';
const CHAT_URL = 'ws://localhost:8888';
export interface LiveTrade {
id: string; // Trade unique ID.
amount: string; // Trade amount.
amount_str: string; // Trade amount represented in string format.
price: string; // Trade price.
price_str: string; // Trade price represented in string format.
type: string; // Trade type (0 - buy; 1 - sell).
timestamp: string; // Trade timestamp.
microtimestamp: string; // Trade microtimestamp.
buy_order_id: string; // Trade buy order ID.
sell_order_id: string; // Trade sell order ID.
}
@Injectable({
providedIn: 'root'
})
export class TickerService {
public tickerStreamObservableSubject: Subject<LiveTrade>;
public msgEventStreamObservableSubject: Subject<MessageEvent>;
constructor(wsService: WebsocketService) {
this.msgEventStreamObservableSubject = wsService.getObservableTicker(CHAT_URL).map(
(response: MessageEvent): LiveTrade => {
let data = JSON.parse(response.data);
return data;
}
);
}
}