mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-03 15:10:49 +00:00
A hubub of code to test websockets. To remove and refine
This commit is contained in:
@@ -3,37 +3,36 @@ import * as Rx from 'rxjs/Rx';
|
||||
|
||||
@Injectable()
|
||||
export class WebsocketService {
|
||||
private socket: Rx.Subject<MessageEvent>;
|
||||
constructor() { }
|
||||
|
||||
private subject: Rx.Subject<MessageEvent>;
|
||||
|
||||
public connect(url): Rx.Subject<MessageEvent> {
|
||||
if(!this.socket) {
|
||||
this.socket = this.create(url);
|
||||
}
|
||||
|
||||
return this.socket;
|
||||
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);
|
||||
(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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user