Skip to content

Instantly share code, notes, and snippets.

@kpavlovsky
Created December 29, 2017 14:32
Show Gist options
  • Save kpavlovsky/e1329e718f9ea22a5e883e7301a1c6a7 to your computer and use it in GitHub Desktop.
Save kpavlovsky/e1329e718f9ea22a5e883e7301a1c6a7 to your computer and use it in GitHub Desktop.
public start(url: string): void {
console.log("connecting to " + url);
let self = this;
this.url = url;
this.ws = Observable.webSocket(this.url);
this.socket = this.ws.subscribe({
next: (data: MessageEvent) => {
if (data['type'] == 'welcome') {
self.opened.next(true);
}
this.message.next(data);
},
error: () => {
console.log('failed to connect');
self.opened.next(false);
this.message.next({ type: 'closed' });
self.socket.unsubscribe();
// setTimeout( () => {
// self.start( self.url );
// }, 1000 );
},
complete: () => {
console.log('Connection to server terminated');
this.message.next({ type: 'closed' });
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment