bfx-api-node-core
Version:
Core Bitfinex Node API
38 lines (31 loc) • 939 B
JavaScript
const debug = require('debug')('bfx:api:manager:events:ws:open')
const authWS = require('../../ws2/auth')
const flushSendBuffer = require('../../ws2/flush_send_buffer')
/**
* Notifies that the connection is opened, flushes the send buffer, and
* authenticates if auth credentials are available
*
* @param {Manager} m
* @param {number} id - websocket ID
*/
module.exports = (m, id) => {
debug('connection opened (%d)', id)
const prevWSState = m.getWS(id)
const ws = flushSendBuffer({
...prevWSState,
isOpen: true,
authenticated: false
})
m.updateWS(id, ws)
m.notifyPlugins('ws2', 'ws', 'open', { id })
m.emit('ws2:open', { id })
const { authToken, apiKey, apiSecret } = ws.authArgs || {}
if (authToken || (apiKey && apiSecret)) {
authWS(ws).then(() => {
debug('authenticated socket %d', id)
}).catch((err) => {
debug('failed to authenticate: %j', err)
})
}
}