Skip to content

Commit

Permalink
Don't send /meta/subscribe before reception of successful /meta/connect
Browse files Browse the repository at this point in the history
Also don't start the websocket (tcp connection) before we have a need
of it as it increases the chance of failure during silent time.
  • Loading branch information
gustafg committed Oct 26, 2017
1 parent 8031f99 commit 5b543d1
Showing 1 changed file with 40 additions and 45 deletions.
85 changes: 40 additions & 45 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class Avanza extends EventEmitter {

constructor() {
super()
this._socket = new WebSocket(SOCKET_URL)
this._socket = null
this._authenticated = false
this._authenticationSession = null
this._authenticationTimeout = MAX_INACTIVE_MINUTES
Expand All @@ -179,12 +179,21 @@ class Avanza extends EventEmitter {
this._customerId = null
this._securityToken = null

this._socketAuthenticating = false
this._socketInitialized = false
this._socketConnected = false
this._socketMessageCount = 1
this._socketClientId = null
}

_socketInit() {
if (this._socket) {
return;
}

this._socket = new WebSocket(SOCKET_URL)
this._socket.on('open', () => { this._authenticateSocket() })
this._socket.on('message', (data) => { this._socketHandleMessage(data) })
}

_socketSend(data) {
if (this._socket && this._socket.readyState === this._socket.OPEN) {
this._socket.send(JSON.stringify([data]))
Expand All @@ -209,10 +218,10 @@ class Avanza extends EventEmitter {
connectionType: 'websocket',
id: this._socketMessageCount,
})
this.emit('handshake')
break
case '/meta/connect':
if (message.successful) {
this._socketConnected = true
this.emit('connect', message)
this._socketSend({
channel: '/meta/connect',
Expand All @@ -230,32 +239,18 @@ class Avanza extends EventEmitter {
}

_authenticateSocket() {
if (!this._socketInitialized && !this._socketAuthenticating) {
this._socketAuthenticating = true
this._socket.on('message', (data) => this._socketHandleMessage(data))

if (this._socket.readyState === this._socket.OPEN) {
this.once('handshake', () => {
this._socketAuthenticating = false
this._socketInitialized = true
})

this._socketSend({
advice: {
timeout: 60000,
interval: 0
},
channel: '/meta/handshake',
ext: { subscriptionId: this._pushSubscriptionId },
id: this._socketMessageCounter,
minimumVersion: '1.0',
supportedConnectionTypes: ['websocket', 'long-polling', 'callback-polling'],
version: '1.0'
})
} else {
this._socket.on('open', () => this._authenticateSocket())
}
}
this._socketSend({
advice: {
timeout: 60000,
interval: 0
},
channel: '/meta/handshake',
ext: { subscriptionId: this._pushSubscriptionId },
id: this._socketMessageCounter,
minimumVersion: '1.0',
supportedConnectionTypes: ['websocket', 'long-polling', 'callback-polling'],
version: '1.0'
})
}

/**
Expand Down Expand Up @@ -499,23 +494,23 @@ class Avanza extends EventEmitter {
}
}

if (!this._socketInitialized) {
this.once('connect', () => this.subscribe(channel, ids, callback))
if (!this._socketAuthenticating) {
this._authenticateSocket()
}
return
if (!this._socket) {
this._socketInit()
}

const subscriptionString = `/${channel}/${ids}`
this.on(subscriptionString, data => callback(data))

this._socketSend({
channel: '/meta/subscribe',
clientId: this._socketClientId,
id: this._socketMessageCount,
subscription: subscriptionString
})
if (!this._socketConnected) {
this.once('connect', () => this.subscribe(channel, ids, callback))
} else {
const subscriptionString = `/${channel}/${ids}`
this.on(subscriptionString, data => callback(data))

this._socketSend({
channel: '/meta/subscribe',
clientId: this._socketClientId,
id: this._socketMessageCount,
subscription: subscriptionString
})
}
}

/**
Expand Down

0 comments on commit 5b543d1

Please sign in to comment.