-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
53 lines (39 loc) · 1.09 KB
/
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
var events = require('events');
var util = require('util');
var Session = require('./session');
function Client (address) {
events.EventEmitter.call(this);
this._address = address;
this._session = null;
this.__onerror = this._onerror.bind(this);
this.__onclose = this._onclose.bind(this);
}
// Client emits: ['close', 'error'] events
util.inherits(Client, events.EventEmitter);
Client.prototype.connect = function(callback) {
var self = this;
callback = callback || Function();
Session.dial(this._address, function(err, session) {
if(err) {
callback(err);
return;
}
self._session = session;
self._session.on('error', self.__onerror);
self._session.on('close', self.__onclose);
callback(null);
});
};
Client.prototype.call = function(name, payload, callback) {
this._session.call(name, payload, callback);
};
Client.prototype.close = function() {
this._session.close();
};
Client.prototype._onerror = function(err) {
this.emit('error', err);
};
Client.prototype._onclose = function(err) {
this.emit('close');
};
module.exports = Client;