Giter Club home page Giter Club logo

blue-frog-b's Introduction

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 hyp = hyperquest.post('http://0.0.0.0:9999/rpc')
    var b   = client()

    hyp.setHeader('content-type', 'application/json')
    hyp.on('error', onError)
    b.on('error', onError)

    b
    .pipe(new rpc.request.BatchStream(true)).on('error', onError)
    .pipe(hyp)
    .pipe(b)

    var sum = b.request('sum', [1,2,3])
    var add = b.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
    })

    b.batch([sum, add])
}

function onError (err) {
    console.log(err)
    ;('data' in err) && console.log(err.data)
}

author

[email protected]

license

MIT

blue-frog-b's People

Contributors

ishiduca avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.