-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathtransform.js
43 lines (32 loc) · 1.01 KB
/
transform.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
// Use Socket::transform and Socket::restore for message passing.
// http://tim.dysinger.net/posts/2013-09-16-getting-started-with-nanomsg.html
var assert = require('assert');
var should = require('should');
var nano = require('../');
var test = require('tape');
nano.Socket.prototype.transform = function (buf) {
if (!Buffer.isBuffer(buf)) buf = new Buffer(buf);
return Buffer.concat([new Buffer([0x00]), buf]);
}
nano.Socket.prototype.restore = function (buf) {
return Buffer.concat([new Buffer([0xFF]), buf]);
}
test('inproc socket pub sub', function (t) {
t.plan(3);
var pub = nano.socket('pub');
var sub = nano.socket('sub');
var addr = 'inproc://pubsub';
var msg = 'hello world';
pub.bind(addr);
sub.connect(addr);
sub.on('message', function (buf) {
t.equal(buf.slice(2).toString(), msg);
t.equal(buf[0], 0xFF);
t.equal(buf[1], 0x00);
pub.close();
sub.close();
});
setTimeout(function () {
pub.send(msg);
}, 100);
});