-
Notifications
You must be signed in to change notification settings - Fork 156
/
rep.js
58 lines (44 loc) · 929 Bytes
/
rep.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
/**
* Module dependencies.
*/
var Socket = require('./sock')
, debug = require('debug')('axon:rep');
/**
* Expose `RepSocket`.
*/
module.exports = RepSocket;
/**
* Initialize a new `RepSocket`.
*
* @api private
*/
function RepSocket() {
Socket.call(this);
}
/**
* Inherits from `Socket.prototype`.
*/
RepSocket.prototype.__proto__ = Socket.prototype;
/**
* Incoming.
*
* @param {net.Socket} sock
* @return {Function} closure(msg, mulitpart)
* @api private
*/
RepSocket.prototype.onmessage = function(sock){
var self = this;
return function (msg, multipart){
if (!multipart) return debug('rep expects multipart');
var id = msg.pop();
msg.unshift('message');
msg.push(reply);
self.emit.apply(self, msg);
function reply() {
var args = [].slice.call(arguments);
args[0] = args[0] || null;
args.push(id);
sock.write(self.pack(args));
}
};
};