forked from askmike/cryptsy-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptsy.js
60 lines (45 loc) · 1.29 KB
/
cryptsy.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
var Pusher = require('pusher-node-client').PusherClient;
var bindAll = require('lodash.bindall');
var Cryptsy = function(market, prefix) {
bindAll(this);
this.channel_prefix = prefix ? prefix : 'trade';
this.client = new Pusher({
key: 'cb65d0a7a72cd94adf1f',
appId: '',
secret: ''
});
this.queue = [];
this.connected = false;
if(market)
this.subscribe(market, prefix);
this.client.on('connect', this._subscribeQueue);
this.client.connect();
}
var util = require('util');
var EventEmitter = require('events').EventEmitter;
util.inherits(Cryptsy, EventEmitter);
// subscribe if connected, else queue until
// we are connected
Cryptsy.prototype.subscribe = function(market) {
// if array, recurse
if(market instanceof Array)
return market.forEach(this.subscribe);
var chanName = this.channel_prefix + '.' + market;
if(this.connected)
this._subscribe(chanName);
else
this.queue.push(chanName);
};
Cryptsy.prototype._subscribeQueue = function() {
this.connected = true;
this.queue.forEach(this._subscribe);
this.queue = [];
}
Cryptsy.prototype._subscribe = function(market) {
this.client.subscribe(market)
.on('message', this.handle);
}
Cryptsy.prototype.handle = function(e) {
this.emit(this.channel_prefix, e);
};
module.exports = Cryptsy;