-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebsocketServer.js
144 lines (126 loc) · 4.25 KB
/
websocketServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const WebSocket = require('ws')
const {
axiosBA,
publicKey
} = require('./axios');
const {
WEBSOCKET_TICKER_URL
} = require('./constants')
class websocketServer {
/**
* Function for request a ticket to authorize a connection
*/
async loadTicket() {
const url = 'websocket/get_ticket'
let response = await axiosBA(`/${url}`)
let ticket = response.data['ticket']
console.log(`loadTicket: ${ticket}`);
return ticket
}
/**
* Constructing a server
* @param {number} port
*/
constructor(port) {
this.resultData = {};
this.wsServer = new WebSocket.Server({
port: port
});
this.wsBA = undefined;
this.subscriptionMessage = {
event: "message",
data: {
operation: "subscribe",
options: {
currency: "BTCUSD",
symbol_set: "global"
}
}
}
this.unsubscriptionMessage = {
event: "message",
data: {
operation: "unsubscribe",
options: {
currency: "BTCUSD",
symbol_set: "global"
}
}
}
this.wsServer.on('connection', function connection(incomingWS) {
incomingWS.onmessage = message => {
console.log(message);
}
console.log('LOG: New WebSocket connection.');
})
this.wsServer.on('close', function close(error) {
console.log(`LOG: WebSocket closed. ${error.code} ${error.reason}`);
this.unsubscribe();
this.wsBA.close();
})
this.wsServer.on('error', (error) => {
console.error('ERROR: ', error.code + error.message)
})
}
/**
* Subscribe function is used for create a new Websocket connection with Bitcoin Average
* @param {string} crypto cryptocurrency (BTC, ETH, etc.)
* @param {string} currency currency (USD, EUR, etc.)
*/
async subscribe(crypto, currency) {
try {
console.log(crypto, currency);
// this.updateSubscriptionMessage(crypto, currency);
/**
* Creating local connection to BA wss with ticket
*/
this.ticket = await this.loadTicket();
this.wsBA = new WebSocket(`${WEBSOCKET_TICKER_URL}?ticket=${this.ticket}&public_key=${publicKey}`);
/**
* Callback function calls on open Bitcoing Average websocket connection
*/
this.wsBA.onopen = () => {
console.log('connection with BA is opened');
this.wsBA.send(JSON.stringify(this.subscriptionMessage));
};
/**
* Callback function for incoming message from Bitcoin Average websocket connection
*/
this.wsBA.onmessage = event => {
const {
data
} = event;
this.resultData = data;
console.log(data);
// Sending data to all of local websocket server clients
this.wsServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
};
});
};
/**
* On closing websocket connection with BitcoinAverage API
*/
this.wsBA.onclose = event => {
console.log(`Websocket connection closed with ${event.code} code`)
}
} catch (error) {
console.error(error)
}
}
/**
* Stop the data stream by websockets (sending unsubscribe message)
* Closing connection to BitcoinAverage
* @param {string} crypto
* @param {string} currency
*/
unsubscribe(crypto, currency) {
this.wsBA.send(JSON.stringify(this.unsubscriptionMessage));
}
updateSubscriptionMessage(crypto, currency) {
this.subscriptionMessage.data.options.currency = `${crypto}${currency}`
this.unsubscriptionMessage.data.operation.currency = `${crypto}${currency}`
}
}
module.exports = websocketServer