diff --git a/.gitignore b/.gitignore index aa6cefb..d5cbd3d 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,6 @@ old/ # dist folder only deployed on npm dist/ + +#Visual Studio +.vs/ \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 6407e94..1c4d80b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -360,12 +360,24 @@ class Avanza extends EventEmitter { _socketSubscribe(subscriptionString) { this._socketSubscriptions[subscriptionString] = null if (this._socketConnected) { - this._socketSend({ - channel: '/meta/subscribe', - clientId: this._socketClientId, - id: this._socketMessageCount, - subscription: subscriptionString - }) + const subscriptionStrings = subscriptionString.split(',') + if (subscriptionStrings.length > 1) { + subscriptionStrings.forEach((subString) => { + this._socketSend({ + channel: '/meta/subscribe', + clientId: this._socketClientId, + id: this._socketMessageCount, + subscription: subString + }) + }) + } else { + this._socketSend({ + channel: '/meta/subscribe', + clientId: this._socketClientId, + id: this._socketMessageCount, + subscription: subscriptionString + }) + } } } @@ -666,6 +678,7 @@ class Avanza extends EventEmitter { throw new Error('Expected to be authenticated before subscribing.') } + const subscriptionStrings = [] if (Array.isArray(ids)) { if ( channel === Avanza.ORDERS || @@ -673,18 +686,34 @@ class Avanza extends EventEmitter { channel === Avanza.POSITIONS ) { ids = ids.join(',') + const subscriptionString = `/${channel}/${ids}` + subscriptionStrings.push(subscriptionString) + } else if ( + channel === Avanza.QUOTES || + channel === Avanza.TRADES + ) { + ids.forEach((id) => { + const subscriptionString = `/${channel}/${id}` + subscriptionStrings.push(subscriptionString) + }) } else { throw new Error(`Channel ${channel} does not support multiple ids as input.`) } + } else { + const subscriptionString = `/${channel}/${ids}` + subscriptionStrings.push(subscriptionString) } + const _this = this; + subscriptionStrings.forEach((subscriptionString) => { + _this.on(subscriptionString, data => callback(data)) + }) + if (!this._socket) { this._socketInit() } - const subscriptionString = `/${channel}/${ids}` - this.on(subscriptionString, data => callback(data)) - this._socketSubscribe(subscriptionString) + this._socketSubscribe(subscriptionStrings) } /**