Skip to content

Instantly share code, notes, and snippets.

@rhodey rhodey/muxrpc-tcp.js
Created May 16, 2018

Embed
What would you like to do?
trying to understand how to use muxrpc atop tcp.
var pull = require('pull-stream')
var toPull = require('stream-to-pull-stream')
var net = require('net')
var MRPC = require('muxrpc')
var API = { echo : 'async' }
var server = undefined
var client = undefined
function echo(str, cb) {
cb(null, str)
}
function listen(port) {
return new Promise((res, rej) => {
server = net.createServer(function (sock) {
var rpc = MRPC(null, API) ({ echo : echo })
rpc.on('closed', () => console.log('server.rpc.closed'))
sock.on('close', () => console.log('server.sock.close'))
sock.on('error', (err) => { console.log('server.sock.error', err); sock.destroy() })
var psock = toPull.duplex(sock)
pull(psock, rpc.createStream(), psock)
})
server.on('listening', () => res(port))
server.on('close', () => console.log('server.close'))
server.on('error', (err) => {
console.log('server.error', err)
server.close()
rej(err)
})
server.listen(port)
})
}
function connect(port) {
client = MRPC(API, null) ()
var sock = net.connect(port)
return new Promise((res, rej) => {
sock.on('connect', () => res())
client.on('closed', () => console.log('client.rpc.closed'))
sock.on('close', () => console.log('client.sock.close'))
sock.on('error', (err) => {
console.log('client.sock.error', err)
sock.destroy()
rej(err)
})
var psock = toPull.duplex(sock)
pull(psock, client.createStream(), psock)
})
}
function cb(err, str) {
if (err) {
console.log('error', err)
} else {
console.log('echo -> ', str)
}
}
listen(2181).then(connect).then(() => {
client.echo('111', cb)
client.echo('222', cb)
client.echo('333', cb)
client.close()
})
/*
$ npm install muxrpc pull-stream stream-to-pull-stream
$ node muxrpc-tcp.js
error { Error: parent stream closed
at Mux.abort.Mux.end (/home/rhodey/dev/dsp/js/pull-debug/node_modules/push-mux/index.js:176:12)
at Object.ws.close (/home/rhodey/dev/dsp/js/pull-debug/node_modules/muxrpc/stream.js:105:8)
at EventEmitter.emitter.close (/home/rhodey/dev/dsp/js/pull-debug/node_modules/muxrpc/index.js:74:8)
at listen.then.then (/home/rhodey/dev/dsp/js/pull-debug/muxrpc-tcp.js:66:10)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7) code: 'PUSH-MUX_CLOSED' }
error { Error: parent stream closed
at Mux.abort.Mux.end (/home/rhodey/dev/dsp/js/pull-debug/node_modules/push-mux/index.js:176:12)
at Object.ws.close (/home/rhodey/dev/dsp/js/pull-debug/node_modules/muxrpc/stream.js:105:8)
at EventEmitter.emitter.close (/home/rhodey/dev/dsp/js/pull-debug/node_modules/muxrpc/index.js:74:8)
at listen.then.then (/home/rhodey/dev/dsp/js/pull-debug/muxrpc-tcp.js:66:10)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7) code: 'PUSH-MUX_CLOSED' }
error { Error: parent stream closed
at Mux.abort.Mux.end (/home/rhodey/dev/dsp/js/pull-debug/node_modules/push-mux/index.js:176:12)
at Object.ws.close (/home/rhodey/dev/dsp/js/pull-debug/node_modules/muxrpc/stream.js:105:8)
at EventEmitter.emitter.close (/home/rhodey/dev/dsp/js/pull-debug/node_modules/muxrpc/index.js:74:8)
at listen.then.then (/home/rhodey/dev/dsp/js/pull-debug/muxrpc-tcp.js:66:10)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7) code: 'PUSH-MUX_CLOSED' }
client.rpc.closed
server.rpc.closed
client.sock.close
server.sock.error { Error: write EPIPE
at _errnoException (util.js:1022:11)
at WriteWrap.afterWrite [as oncomplete] (net.js:867:14) code: 'EPIPE', errno: 'EPIPE', syscall: 'write' }
server.sock.close
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.