blue-frog-b
v0.1.3
Published
create a JSON-RPC 2.0 server/client that corresponds to the batch processing
Downloads
3
Maintainers
Readme
blue-frog-b
create a JSON-RPC 2.0 server/client that corresponds to the batch processing.
usage
server
const http = require('http')
const ecstatic = require('ecstatic')(__dirname + '/static')
const rpc = require('blue-frog-b')({prefix: '/rpc'})
const app = http.craeteServer(ecstatic)
function add (a, b) { return Number(a) + Number(b) }
rpc.on('sum', (params, done) => {
setTimeout(() => {
try { done(null, params.reduce(add, 0)) }
catch (err) { done(err) }
}, 500)
})
rpc.on('add', (params, done) => {
setTimeout(() => {
try { done(null, add.apply(null, params)) }
catch (err) { done(err) }
}, 800)
})
rpc.install(app)
app.listen(9999, () => console.log('listen on port %s', app.address().port))
browser
var hyperquest = require('hyperquest')
var rpc = require('blue-frog-stream')
var client = require('blue-frog-b')
window.onload = function () {
var dup = client()
var hyp = hyperquest.post('http://0.0.0.0:9999/rpc')
hyp.setHeader('content-type', 'application/json')
hyp.on('error', onError)
dup.on('error', onError)
dup
.pipe(new rpc.request.BatchStream(true)).on('error', onError)
.pipe(hyp)
.pipe(dup)
var sum = dup.request('sum', [1,2,3])
var add = dup.request('add', [4, 5])
sum.on('error', onError)
add.on('error', onError)
sum.once('data', function (result) {
console.log('sum(1, 2, 3) => ', result)
// 'sum(1, 2, 3) => 6
})
add.once('data', function (result) {
console.log('add(4, 5) => ', result)
// 'add(4, 5) => 9
})
dup.batch([sum, add])
}
function onError (err) {
console.log(err)
;('data' in err) && console.log(err.data)
}
use express
const express = require('express')
const app = express()
const rpc = require('blue-frog-b')({prefix: '/rpc'})
app.use(express.static('static'))
rpc.on('sum', function (params, done) {
// ...
})
rpc.install(app.listen(9999))
api
browser api
var frog = require('blue-frog-b')
var dup = frog()
return a duplex stream. this stream push JSON-RPC 2.0 object
.
var tadpole = dup.request(method[, params])
var tadpole = dup.notification(method[, params])
dup.batch(tadpolelist)
author
license
MIT