-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (57 loc) · 1.81 KB
/
index.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
var io = require('socket.io-client');
var Promise = require('promise');
var EventEmitter = require('events').EventEmitter;
function WDWSClient(url, options) {
this.url = url;
this.options = options || {};
this.pid = 10000;
this.emitters = [];
}
WDWSClient.prototype.connect = function() {
if (this.socket) {
this.socket.connect();
} else {
this.initializeSocket();
}
}
WDWSClient.prototype.initializeSocket = function() {
this.socket = io(this.url, this.options.socket || {});
this.socket.on('pm', this.dispatchProcessMessage.bind(this));
}
WDWSClient.prototype.dispatchProcessMessage = function(pid, event) {
var args = Array.prototype.slice.call(arguments, 1);
var emitter = this.emitters[pid];
emitter && emitter.emit(args);
}
WDWSClient.prototype.on = function(event, callback) {
if (!this.socket) { throw "ERROR: Can't listen for socket events until after you call connect()"; };
this.socket.on(event, callback);
}
WDWSClient.prototype.disconnect = function(callback) {
this.socket.disconnect();
}
WDWSClient.prototype.nextPid = function() {
return ++this.pid;
}
WDWSClient.prototype.run = function(cmd, params, callback) {
var newPid = this.nextPid();
params.pid = newPid;
if (!this.socket) { throw "ERROR: Must run connect() before executing commands." };
var that = this;
var promise = new Promise(function(resolve, reject) {
that.socket.emit(cmd, params, function(err) {
var rest = Array.prototype.slice.call(arguments, 1);
err ? reject(err) : resolve.apply(null, rest);
});
}).nodeify(callback);
if (params.subscribe) {
var emitter = new EventEmitter();
this.emitters[newPid] = emitter;
promise.on = function() {
emitter.on.apply(emitter, arguments);
return promise;
}
}
return promise;
}
module.exports = WDWSClient;