Skip to content

Instantly share code, notes, and snippets.

@ralphtheninja
Last active December 10, 2015 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphtheninja/4373760 to your computer and use it in GitHub Desktop.
Save ralphtheninja/4373760 to your computer and use it in GitHub Desktop.
mux-demux test
var MuxDemux = require('mux-demux');
var duplex = require('duplex');
var net = require('net');
var server = net.createServer(function (con) {
var mdm2 = MuxDemux(function (stream) {
if (stream.writable && stream.meta === 'times') {
var d = duplex().on('_data', function(data) {
if (data === 'hello') {
this._data('world');
}
else {
this._data('wrong command');
}
});
stream.pipe(d).pipe(stream);
}
});
con.pipe(mdm2).pipe(con);
});
server.listen(8642, function () {
var con = net.connect(8642);
var mdm1 = MuxDemux();
con.pipe(mdm1).pipe(con);
var ds = mdm1.createStream('times');
setInterval(function () {
if (Math.random() > 0.3) {
ds.write('hello');
}
else {
ds.write('blabla');
}
}, 1e2);
ds.on('data', function(data) {
console.log('client got response:', data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment