afrad / angular2-websocket

Websocket wrapper for angular2 based on angular-websocket
Apache License 2.0
210 stars 84 forks source link

Should getDataStream.subscribe continue to fire after re-connection? #78

Closed NadavK closed 7 years ago

NadavK commented 7 years ago

I create a $websocket instance with the reconnect param: {"reconnectIfNotNormalClose": true} and the stream nicely reconnects on failures.

I have a problem with incoming messages: ws.getDataStream().subscribe correctly fires on incoming messages until the websocket reconnects, after which it stops firing.

Am I missing a step so ws.getDataStream().subscribe continues firing after an automatic reconnection?

here is the code (comments welcome, I am new to js):

this.ws = new $WebSocket('wss://...', null, JSON.parse('{"reconnectIfNotNormalClose": true}'));
this.ws.getDataStream().subscribe(
  (msg: MessageEvent) => console.log('message received: ', msg),
  (err) => console.log('incoming websocket err', err),
  () => console.log('incoming websocket complete')
);

I also tried moving the subscribe into the stream's onOpen, but it behaves the same as above (fires only for the initial connection):

this.ws = new $WebSocket('wss://...', null, JSON.parse('{"reconnectIfNotNormalClose": true}'));
this.stream.onOpen(
  this.ws.getDataStream().subscribe(
    (msg: MessageEvent) => console.log('message received: ', msg),
    (err) => console.log('incoming websocket err', err),
    () => console.log('incoming websocket complete')
  )
);
NadavK commented 7 years ago

Can anyone provide a hint?

godzie44 commented 7 years ago

Do you finded the solution of this? Looks like this code in source

this.socket.onerror = (ev: ErrorEvent) => { // console.log('onError ', ev); self.onErrorHandler(ev); this.dataStream.error(ev); };

will internally finalize the observable.

NadavK commented 7 years ago

No, I don't have a solution. Maybe I am not using the package correctly - is there a working sample code?

godzie44 commented 7 years ago

I'm fixed this problem by adding stream for error messages. You can watch my solution in fork.