-
Notifications
You must be signed in to change notification settings - Fork 3
/
subscribe.js
48 lines (37 loc) · 1.17 KB
/
subscribe.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
var domain = require('domain');
var through = require('through2');
var graft = require('graft');
var debug = require('debug');
var log = debug('ab:subscribe');
var logStream = require('debug-stream')(log);
module.exports = function subscribe(merge) {
var merge = merge || graft();
var active = 0;
return through.obj(function(msg, enc, done) {
var client = active++;
log('receive subscribe message: '+client);
var d = domain.create();
d.on('error', function(err) { log('ending of client '+client+' with error: '+err); });
d.run(function() {
msg._session.on('close' , function() {
merge.unpipe(msg.strokeSync);
msg.strokeInput.unpipe();
});
merge.pipe(msg.strokeSync);
msg.strokeInput
.pipe(logStream('incoming stroke %s from: '+client))
.pipe(through.obj(function(chunk, enc, done) {
var msg = {
topic: chunk.topic,
size: chunk.size,
color: chunk.color,
segments: through.obj()
};
chunk.segments.pipe(msg.segments);
merge.write(msg);
done();
}));
done();
});
});
};