-
Notifications
You must be signed in to change notification settings - Fork 0
/
te_client.js
206 lines (132 loc) · 3.75 KB
/
te_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
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
'use strict'
var util = require('util');
var events = require("events");
var WebSocket = require('ws'),
defaultOptions = {
url: 'ws://stream.tradingeconomics.com/',
key: 'guest',
secret: 'guest',
reconnect: true, //reconnect on error/disconnect
reconnect_timeout: 30000, //time (in miliseconds) to wait before attepmt to reconect
onMessage: function(){},
onError: function(){}
};
var TEClient = function(options){
events.EventEmitter.call(this);
this.userOptions = options || {};
this.init.apply(this);
return this;
};
util.inherits(TEClient, events.EventEmitter);
TEClient.prototype.init = function(){
var _this = this;
_this.options = merge(defaultOptions, _this.userOptions);
_this.subArr = []; // ["calendar"]
_this.connect.apply(this);
return _this;
};
TEClient.prototype.connect = function(){
var _this = this,
options = _this.options,
url = buildWsUrl(options);
console.log('\n Connecting to ', url);
_this.ws = new WebSocket(url);
_this.ws.on('open', function(){
console.log('SOCKET CONNECTED');
//subscribe to our list
_this.subArr.forEach(function(subject){
_this.ws.send('{"topic": "subscribe", "to": "'+subject+'"}');
});
});
_this.ws.on('message', function(data){
try{
var aux = JSON.parse(data);
if(aux.topic && aux.topic!='keepalive'){
_this.emit('message', aux);
}
}catch(err){
//console.log(err);
}
});
_this.ws.on('close', function(){
console.log('SOCKET CLOSED');
if(options.reconnect){
console.log('Trying to reconnect in ', options.reconnect_timeout);
setTimeout(function(){
_this.connect();
}, options.reconnect_timeout);
}
});
_this.ws.on('error', function(){
console.log('ERROR');
if(options.reconnect){
console.log('Trying to reconnect in ', options.reconnect_timeout);
setTimeout(function(){
_this.connect();
}, options.reconnect_timeout);
}
});
return _this;
};
TEClient.prototype.subscribe = function(to){
var _this = this;
if(_this.subArr.indexOf(to)<0){
_this.subArr.push(to);
}
if(!_this.ws || _this.ws.readyState != WebSocket.OPEN){
// #todo -> emit error
return _this;
}
_this.ws.send('{"topic": "subscribe", "to": "'+to+'"}');
return _this;
};
module.exports = TEClient;
function buildWsUrl(options){
var url = options.url;
url += ('?client=' + options.key + ':' + options.secret);
return url;
}
/**
* Deep merge two or more objects and return a third object. If the first argument is
* true, the contents of the second object is copied into the first object.
* First, it deep merged arrays, which lead to workarounds in Highcharts. Second,
* it copied properties from extended prototypes.
*/
function merge() {
var i,
args = arguments,
len,
ret = {},
doCopy = function (copy, original) {
var value, key;
// An object is replacing a primitive
if (typeof copy !== 'object') {
copy = {};
}
for (key in original) {
if (original.hasOwnProperty(key)) {
value = original[key];
// Copy the contents of objects, but not arrays or DOM nodes
if (value && typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]' &&
key !== 'renderTo' && typeof value.nodeType !== 'number') {
copy[key] = doCopy(copy[key] || {}, value);
// Primitives and arrays are copied over directly
} else {
copy[key] = original[key];
}
}
}
return copy;
};
// If first argument is true, copy into the existing object. Used in setOptions.
if (args[0] === true) {
ret = args[1];
args = Array.prototype.slice.call(args, 2);
}
// For each argument, extend the return
len = args.length;
for (i = 0; i < len; i++) {
ret = doCopy(ret, args[i]);
}
return ret;
}