-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
209 lines (177 loc) · 4.7 KB
/
server.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var express = require('express');
var WebSocket = require('ws');
var CryptoJS = require("crypto-js");
var path = require('path');
//#region WEbSocket init
//these keys are for demonstration purposes
//never show your real keys
//to create your own keys visit the following page - https://cex.io/#/modal/sign-in
var secretKey = "ycZV8pEnDOrp45YNkKkayWYBZQU";
var apiKey = 'ZTU4IIkSzfpzPz2hTler3giTr6U';
//global respond variable
var globalRes;
//create socket
var appWS = {
socket: null,
started: false,
// open socket
start: function () {
// create new connection
if (appWS.socket === null) {
console.log('start WebSocket');
appWS.socket = new WebSocket('wss://ws.cex.io/ws/');
// set messages listenere
appWS.socket.onmessage = appWS.onmessage;
appWS.socket.onclose = appWS.onclose;
// connection is stable
appWS.socket.onopen = function () {
appWS.auth();
appWS.started = true;
};
}
},
// logging function
log: function (msg) {
console.log(msg);
},
// send messages
send: function (data) {
appWS.log('send: ' + data);
appWS.socket.send(data);
// console.log(data);
},
// messages listener
onmessage: function (e) {
// get answer
var data = JSON.parse(e.data);
// log
appWS.log(e.data);
// console.log(e.data);
//ping-pong handler
if (data.e === 'ping') {
appWS.pong();
}
//auth confirmation
if (data.e === 'auth' && data.data.ok === 'ok') {
appWS.started = true;
appWS.tickerSubscription();
// appWS.pairRoomSubscription();
appWS.OHLCVsubscription();
}
// process data
appWS.processData(data);
},
onclose: function (event) {
if (event.wasClean) {
console.log('Clear close');
} else {
console.log('Connection lost');
}
console.log('Error: ' + event.code + ' reason: ' + event.reason);
},
// process data handler
processData: function (data) {
// BTC/USD current price ticker
if (data.e === 'tick' && data.data.symbol1 === 'BTC' && data.data.symbol2 === 'USD') {
sendDataToHost(globalRes, data);
}
// data for the last 120 minutes
if (data.e === 'init-ohlcv-data' && data.pair === 'BTC:USD') {
sendDataToHost(globalRes, data);
}
// BTC/USD 1 minute changes subscription
if (data.e === 'ohlcv1m' && data.data.pair === 'BTC:USD') {
sendDataToHost(globalRes, data);
}
},
auth: function () {
//get current timestamp
var timestamp = Math.floor(Date.now() / 1000);
//create crypto-ket
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secretKey);
hmac.update(timestamp + apiKey);
var hash = hmac.finalize();
var hex = CryptoJS.enc.Hex.stringify(hash);
//prepare and send auth message
var args = {
e: 'auth', auth: {
key: apiKey,
signature: hex, timestamp: timestamp
}
};
var authMessage = JSON.stringify(args);
appWS.send(authMessage);
},
pong: function () {
appWS.send(JSON.stringify({
e: "pong"
}));
},
//OHLCV charts subscriptions
OHLCVsubscription: function () {
appWS.send(JSON.stringify({
e: "init-ohlcv",
i: "1m",
rooms: [
"pair-BTC-USD"
]
}));
},
// Ticker subscription
tickerSubscription: function () {
appWS.send(JSON.stringify({
"e": "subscribe",
"rooms": [
"tickers"
]
}));
}
};
//#endregion
//#region COMET
//creates COMET connection
function openStreamingToHost(req, res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Cache-Control': 'no-cache'
});
console.log('open stream');
}
function sendDataToHost(res, data) {
console.log('send data');
res.write('data: ' + JSON.stringify(data) + '\n\n');
}
//#endregion
//#region Express init
// Creates server instance
var app = express();
app.use(express.static(path.join(__dirname + '/public')));
//basic get request handler
app.get('/', function (req, res) { //on html request of root directory, run callback function
res.sendFile(path.join(__dirname, '/public/index.html')); //send html file named "index.html"
});
//handler for get request to connect to CEXio
app.get('/cexIO', function (req, res) {
globalRes = res;
openStreamingToHost(req, res);
if (appWS.started === false) {
appWS.start();
} else {
reconnect();
}
});
// Runs express server
app.listen(8081, function () {
console.log('Example app is listening on port ' + 8081 + '!\n');
});
function reconnect() {
try {
appWS.socket.close(1000, 'Reconnecting');
appWS.socket = null;
appWS.start();
} catch (e) {
console.log('Reconnect failed, retrying...');
reconnect();
}
}
//#endregion