-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (57 loc) · 1.77 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
// Generated by IcedCoffeeScript 108.0.11
(function() {
var MsgpackRPC, msgpack;
msgpack = require('msgpack');
MsgpackRPC = (function() {
MsgpackRPC.prototype.id = 0;
MsgpackRPC.prototype.cbs = {};
MsgpackRPC.prototype.methods = {};
function MsgpackRPC(namespace, sock) {
var ms;
this.namespace = namespace;
this.sock = sock;
this.sock.on('drain', function(h) {
return console.log("Draining: " + h);
});
ms = new msgpack.Stream(this.sock);
ms.addListener('msg', this._parse.bind(this));
}
MsgpackRPC.prototype._write = function(data) {
var e;
try {
return this.sock.write(data);
} catch (_error) {
e = _error;
return setTimeout(this._write.bind(this, data), 500);
}
};
MsgpackRPC.prototype.invoke = function(method, params, cb) {
var req;
req = msgpack.pack([0, this.id, "" + this.namespace + "." + method, params]);
this.cbs[this.id++] = cb;
return this._write(req);
};
MsgpackRPC.prototype._parse = function(packet) {
var method;
if (packet[0] === 0) {
method = packet[2].replace("" + this.namespace + ".", '');
method = this.methods[method];
return method(packet[3], (function(_this) {
return function(err, res) {
if (err) {
res = msgpack.pack([1, packet[1], err, null]);
} else {
res = msgpack.pack([1, packet[1], null, res]);
}
return _this._write(res);
};
})(this));
} else if (packet[0] === 1) {
this.cbs[packet[1]](packet[2], packet[3]);
return delete this.cbs[packet[1]];
}
};
return MsgpackRPC;
})();
module.exports = MsgpackRPC;
}).call(this);