I am using RxJS in an angular 4 project.
I am trying to initiate a Websocket, and especially to know when this one is opened.
I am currently using WebSocket from RxJS (v5). https://github.com/ReactiveX/rxjs/blob/master/src/observable/dom/WebSocketSubject.ts
I noticed there is an openObserver in WebSocketSubjectConfig but I cannot find how to create the Observer. I have been locked on it from several hours.
Here is an excerpt of my code so far:
import { Injectable } from '@angular/core';
import { webSocket} from 'rxjs/observable/dom/webSocket';
import { WebSocketSubject, WebSocketSubjectConfig} from 'rxjs/observable/dom/WebSocketSubject';
@Injectable()
export class MzkWebsocketJsonRpcService {
subject: WebSocketSubject<any>;
jsonRpcId: number;
constructor() {
this.subject = webSocket('ws://localhost:12345');
this.subject.openObserver =
/// Find a way to create the openObserver
this.subject.subscribe(
this.onMessage,
this.onError,
this.onClose,
);
console.log('Socket connected');
this.jsonRpcId = 1;
}
public send(method: string, params: any[]) {
let jsonFrame: any = {id: this.jsonRpcId, 'json-rpc': '2.0', method: method};
if (params) {
jsonFrame['params'] = params;
}
this.subject.next(JSON.stringify(jsonFrame));
this.jsonRpcId ++;
}
onMessage(data: string) {
console.log('Websocket message: ', data);
}
onError(data: string) {
console.log('Websocket error:', data);
}
onClose() {
console.log('Websocket closing');
}
}