-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3cqs-signals-client.js
144 lines (81 loc) · 2.25 KB
/
3cqs-signals-client.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
/*
3CQS Signals Client Sample
https://www.3cqs.com
(NOT FOR DISTRIBUTION)
Requirements: Node.JS + socket.io-client
How to run:
1. Install Socket.IO client: npm i socket.io-client
2. Open terminal / command prompt window
3. Type: node 3cqs-signals-client.js
*/
const version = '0.7';
let fatalError = false;
const socket = require('socket.io-client')('https://stream.3cqs.com', {
extraHeaders: {
'api-key': '',
'user-agent': '3CQS Signal Client/' + version
},
transports: ['websocket', 'polling'],
path: '/stream/v1/signals',
reconnection: true,
reconnectionDelay: 10000
});
socket.on('connect', (data) => {
showLog('Connected to 3CQS Signals');
let messageId = randomString(15);
// Get max last n signals
setTimeout(() => {
socket.emit('signal_history', { 'max': 10, 'message_id_client': messageId });
}, 500);
});
socket.on('signal', (data) => {
let content = 'SIGNAL RECEIVED';
if (data['created_history'] != undefined && data['created_history'] != null && data['created_history'] != '') {
content += ' (HISTORY)';
}
showLog(content);
console.log(data);
});
socket.on('info', (data) => {
showLog('INFO');
console.log(data);
});
socket.on('error', (data) => {
if (data['code'] == 403) {
fatalError = true;
}
showLog('ERROR');
console.log(data);
});
socket.on('connect_error', (data) => {
fatalError = true;
showLog('CONNECT_ERROR');
console.log(data);
});
socket.on('connect_failed', (data) => {
fatalError = true;
showLog('CONNECT_FAILED');
console.log(data);
});
socket.on('disconnect', (reason) => {
showLog('Client Disconnected: ' + reason);
//Either 'io server disconnect' or 'io client disconnect'
if (!fatalError && reason === 'io server disconnect') {
showLog('Server disconnected the client. Trying to reconnect.');
// Disconnected by server,so reconnect again
socket.connect();
}
});
function randomString(len) {
let chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let str = '';
for (let i = 0; i < len; i++) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
}
function showLog(content) {
let contentFormat = '<----{CONTENT}---->';
content = contentFormat.replace(/\{CONTENT\}/g, content);
console.log(content);
}