Created
July 20, 2013 12:44
-
-
Save nakosung/6044876 to your computer and use it in GitHub Desktop.
mux-demux dnode net
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
es = require 'event-stream' | |
dnode = require 'dnode' | |
MuxDemux = require 'mux-demux' | |
net = require 'net' | |
assert = require 'assert' | |
D = (send,recv) -> | |
d = dnode fn : (cb) -> cb(send) | |
d.on 'remote', (r) -> | |
assert r.fn | |
r.fn (x) -> | |
assert x == recv | |
console.log 'pass', send, recv | |
d | |
mux_client2 = (m1) -> | |
m2 = MuxDemux() | |
ps2 = (require 'pause-stream')() | |
ps2.pause() | |
c = m2.createStream 'any' | |
b = D 'b', 'a' | |
es.pipeline c, b, ps2, c | |
c.once 'data', -> ps2.resume() | |
es.pipeline(m2,m1,m2) | |
m2.resume() | |
proxy = (next) -> | |
h = (c) -> | |
console.log 'server got connection' | |
c.setEncoding 'utf-8' | |
a = D 'a', 'b' | |
es.pipeline a, c, a | |
net.createServer(h).listen 3000, -> | |
fn = (next) -> | |
p = net.connect 3000, -> | |
console.log 'client connected' | |
p.setEncoding 'utf-8' | |
next(p) | |
next fn | |
mux_proxied = -> | |
proxy (fac_a) -> | |
m1 = MuxDemux (c) -> | |
console.log 'mux req.d', c.meta | |
fac_a (a) -> | |
es.pipeline c, a, c | |
mux_client2 m1 | |
mux_proxied() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment