Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions lib/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@ const Sockets = {}
Sockets.ws = {}
Sockets.heartbeat = {}

getPublicWsToken = async function(baseURL) {
const getPublicWsToken = async function(baseURL) {
let endpoint = '/api/v1/bullet-public'
let url = baseURL + endpoint
let result = await axios.post(url, {})
return result.data
}

getPrivateWsToken = async function(baseURL, sign) {
const getPrivateWsToken = async function(baseURL, sign) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this means :)

let endpoint = '/api/v1/bullet-private'
let url = baseURL + endpoint
let result = await axios.post(url, {}, sign)
return result.data
}

getSocketEndpoint = async function(type, baseURL, environment, sign) {
const getSocketEndpoint = async function(type, baseURL, environment, sign) {
let r
if (type == 'private') {
r = await getPrivateWsToken(baseURL, sign)
} else {
r = await getPublicWsToken(baseURL)
}

let token = r.data.token
let instanceServer = r.data.instanceServers[0]

if(instanceServer){
if (environment === 'sandbox') {
return `${instanceServer.endpoint}?token=${token}&[connectId=${Date.now()}]`
return `${instanceServer.endpoint}/endpoint?token=${token}&[connectId=${Date.now()}]`
} else if (environment === 'live') {
return `${instanceServer.endpoint}?token=${token}&[connectId=${Date.now()}]`
}
Expand All @@ -48,31 +49,41 @@ getSocketEndpoint = async function(type, baseURL, environment, sign) {
}
eventHanlder = function
*/

Sockets.getSocketDictKey = function(topic, symbols){
return `${topic}:${JSON.stringify(symbols)}`
}

Sockets.initSocket = async function(params, eventHandler) {
try {
if ( !params.sign ) params.sign = false;
if ( !params.endpoint ) params.endpoint = false;
let [topic, endpoint, type] = Sockets.topics( params.topic, params.symbols, params.endpoint, params.sign )
const wsKey = Sockets.getSocketDictKey(topic, params.symbols);

let sign = this.sign('/api/v1/bullet-private', {}, 'POST')
let websocket = await getSocketEndpoint(type, this.baseURL, this.environment, sign)
let ws = new WebSocket(websocket)
Sockets.ws[topic] = ws
Sockets.ws[wsKey] = ws
ws.on('open', () => {
console.log(topic + ' opening websocket connection... ')
Sockets.subscribe(topic, endpoint, type, eventHandler)
Sockets.ws[topic].heartbeat = setInterval(Sockets.socketHeartBeat, 20000, topic)
console.log(wsKey + ' opening websocket connection... ')
Sockets.subscribe(wsKey, endpoint, type, eventHandler)
Sockets.ws[wsKey].heartbeat = setInterval(Sockets.socketHeartBeat, 1000, wsKey)
})
ws.on('error', (error) => {
Sockets.handleSocketError(error)
console.log(error)
})
ws.on('ping', () => {
ws.on('ping', (cancel, data) => {
ws.send(data)
return
})
ws.on('close', () => {
clearInterval(Sockets.ws[topic].heartbeat)
console.log(topic + ' websocket closed...')
clearInterval(Sockets.ws[wsKey].heartbeat)
console.log(wsKey + ' websocket closed...')
})

return () => ws.close();
} catch (err) {
console.log(err)
}
Expand All @@ -85,7 +96,7 @@ Sockets.handleSocketError = function(error) {

Sockets.socketHeartBeat = function(topic) {
let ws = Sockets.ws[topic]
ws.ping()
ws.ping(["ping"])
}

Sockets.subscribe = async function(topic, endpoint, type, eventHandler) {
Expand Down