fhqvst / avanza

A JavaScript client for the unofficial Avanza API
MIT License
228 stars 59 forks source link

WebSocket enhancements #11

Closed gustafg closed 6 years ago

gustafg commented 6 years ago

This is really 2 different commits. Sorry about that. I can attempt to split them up another day but I didn't have the time and energy right now.

Also the state handling is not extremely pretty.. maybe can be written better but I wanted to commit something now so I can proceed to the next thing. You take it if you want it otherwise I'm happy to discuss how it could be written better.

Two changes:

  1. The WebSocket session expires after 24h, when this happens Avanza will send out a /meta/disconnect after which a new handshake must be performed before any new operation can take place on the socket. This code will initiate a new handshake upon reception of /meta/disconnect or advice.reconnect='handshake'. Note that after session expiry all subscriptions are lost.

  2. In the event the TCP socket drops the 'ws' library will send an 'close' event after which a new object must be instantiated to create a new TCP connection. Once this connection is up a /meta/connect will be sent out and subscriptions will continue to function.

gustafg commented 6 years ago

I think you shouldn't merge this one. Pretty sure I will have something better soon. Keeping it open in case you want to discuss something for now.