Global

Methods

WComorHapiClient(opt) → {Promise}

Source:

建立http API使用者(Node.js與Browser)端物件

Example
import WComorHapiClient from 'w-comor-hapi/dist/ws-client-node.umd.js'

//opt
let opt = {
    url: 'http://localhost:8080/api',
    token: '*',
        error: function(err) {
            console.log('client: error:', err)
        },
        reconn: function() {
            console.log('client: reconn')
        },
}

//WComorHapiClient
new WComorHapiClient(opt)
    .then(function(wo) {
        console.log('client: funcs: ', wo)

        function core(ps) {
            wo.group.plus(ps)
                .then(function(r) {
                    console.log('client: plus(' + JSON.stringify(ps) + ')=' + r)
                })
                .catch(function(err) {
                    console.log('client: plus: catch: ', err)
                })
            wo.group.div(ps)
                .then(function(r) {
                    console.log('client: div(' + JSON.stringify(ps) + ')=' + r)
                })
                .catch(function(err) {
                    console.log('client: div: catch: ', err)
                })
            wo.add(ps)
                .then(function(r) {
                    console.log(`client: add(${JSON.stringify(ps)})=${r}`)
                })
                .catch(function(err) {
                    console.log('client: add: catch: ', err)
                 })
            wo.minu(ps)
                .then(function(r) {
                    console.log(`client: minu(${JSON.stringify(ps)})=${r}`)
                })
                .catch(function(err) {
                    console.log('client: minu: catch: ', err)
                 })
        }

        let i = 100
        setInterval(function() {
            i += 1
            core({
                p1: i,
                p2: 10,
            })
        }, 1000)

    })
    .catch(function(err) {
        console.log('client: catch', err)
    })
Parameters:
Name Type Description
opt Object

輸入設定參數物件

Properties
Name Type Attributes Default Description
url String <optional>
'http://localhost:8080/api'

輸入http API伺服器網址,預設為'http://localhost:8080/api'

token String <optional>
'*'

輸入使用者認證用token,預設為'*'

error function

輸入監聽error函數

reconn function

輸入監聽reconn函數

Returns:

回傳Promise,resolve為映射伺服器端可用函數之物件,各函數輸入皆為單一物件,各函數回傳皆為Promise,用resolve與reject處理回傳結果

Type
Promise

WComorHapiServer(opt)

Source:

建立http API伺服器

Example
import WComorHapiServer from 'w-comor-hapi/dist/w-comor-hapi-server.umd.js'

function random(min, max) {
    return Math.floor(Math.random() * max) + min
}

let opt = {
    port: 8080,
    apiName: 'api',
    authenticate: function(token) {
        //使用token驗證使用者身份
        return new Promise(function(resolve, reject) {
            setTimeout(function() {
                resolve(true)
            }, 1000)
        })
    },
    filterFuncs: function(token, funcs) {
        //使用token驗證使用者身份與過濾可用funcs
        return new Promise(function(resolve, reject) {
            funcs = funcs.filter(function(v) {
                return v.indexOf('Hide') < 0
            })
            resolve(funcs)
        })
    },
    onClientChange: function(conns, opt) {
        console.log(`Server[port:${opt.port}] now conns: ${conns.length}`)
    },
    funcs: {
        'group.plus': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 * p2)
                }, random(100, 3000))
            })
        },
        'group.div': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 / p2)
                }, random(100, 3000))
            })
        },
        'add': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 + p2)
                }, random(100, 3000))
            })
        },
        'addHide': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 + p2)
                }, random(100, 3000))
            })
        },
        'minu': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 - p2)
                }, random(100, 3000))
            })
        },
    },
    routes: [
        {
            method: 'GET',
            path: '/code',
            handler: function (req, res) {
                //console.log(req)
                return 'get code query: ' + JSON.stringify(req.query)
                //http://localhost:8080/code?a=1&bb=23.45
                // => get code query: {"a":"1","bb":"23.45"}
            }
        },
    ],
}

new WComorHapiServer(opt)
Parameters:
Name Type Description
opt Object

輸入設定參數物件

Properties
Name Type Attributes Default Description
serverHapi Object <optional>
{}

輸入hapi伺服器物件,若提供,本服務將自動加入api至route。使用外部hapi伺服器時,需開啟跨域功能,或是使用nginx反向代理轉入api請求

port Integer <optional>
8080

輸入http API伺服器所在port,預設8080

apiName String <optional>
'api'

輸入http API伺服器網址的api名稱,預設'api'

authenticate function

輸入使用者身份認證函數,供伺服器端驗證之用,函數會傳入使用者端連線之token參數,回傳為Promise,resolve(true)為驗證通過,resolve(false)為驗證不通過

funcs Object <optional>
{}

輸入伺服器端供使用者端呼叫之函數物件,各key為函數名稱,對應value為函數本體。各函數之輸入需為單一物件,而各函數回傳皆為Promise,可通過resolve與reject回傳結果,預設{}

routes Array <optional>
[]

輸入伺服器額外掛載routes陣列,預設[]