WConverhpClient

WConverhpClient

new WConverhpClient(opt) → {Object}

Source:

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

Example
import WConverhpClient from 'w-converhp/dist/w-converhp-client.umd.js'

let opt = {
    url: 'http://localhost:8080',
    apiName: 'api',
}

//new
let wo = new WConverhpClient(opt)

wo.on('open', function() {
    console.log('client nodejs: open')
})
wo.on('openOnce', function() {
    console.log('client nodejs: openOnce')

    //p
    let name = 'zdata.b1'
    let p = {
        a: 12,
        b: 34.56,
        c: 'test中文',
        d: {
            name: name,
            u8a: new Uint8Array([66, 97, 115]),
            //u8a: new Uint8Array(fs.readFileSync('C:\\Users\\Administrator\\Desktop\\'+name)),
        }
    }

    //execute
    wo.execute('add', { p },
        function (prog, p, m) {
            console.log('client nodejs: execute: prog', prog, p, m)
        })
        .then(function(r) {
            console.log('client nodejs: execute: add', r)
        })
        .catch(function(err) {
            console.log('client nodejs: execute: catch', err)
        })

    //broadcast
    wo.broadcast('client nodejs broadcast hi', function (prog) {
        console.log('client nodejs: broadcast: prog', prog)
    })
        .catch(function(err) {
            console.log('client nodejs: broadcast: catch', err)
        })

    //deliver
    wo.deliver('client nodejs deliver hi', function (prog) {
        console.log('client nodejs: deliver: prog', prog)
    })
        .catch(function(err) {
            console.log('client nodejs: deliver: catch', err)
        })

})
wo.on('close', function() {
    console.log('client nodejs: close')
})
wo.on('error', function(err) {
    console.log('client nodejs: error', err)
})
wo.on('reconn', function() {
    console.log('client nodejs: reconn')
})
wo.on('broadcast', function(data) {
    console.log('client nodejs: broadcast', data)
})
wo.on('deliver', function(data) {
    console.log('client nodejs: deliver', data)
})
Parameters:
Name Type Description
opt Object

輸入設定參數物件

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

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

apiName String <optional>
'api'

輸入api名稱,預設為'api'

timePolling Integer <optional>
2000

輸入輪詢間隔時間整數,單位為毫秒,預設為2000

retry Integer <optional>
3

輸入傳輸失敗重試次數整數,預設為3

Returns:

回傳通訊物件,可監聽事件open、openOnce、close、error、reconn、broadcast、deliver,可使用函數execute、broadcast、deliver

Type
Object

Methods

(static) broadcast(data)

Source:

Hapi通訊物件對伺服器端廣播函數,表示傳送資料給伺服器,還需轉送其他客戶端

Example
let data = {...}
wo.broadcast(data)
Parameters:
Name Type Description
data *

輸入廣播函數之輸入資訊

(static) deliver(data)

Source:

Hapi通訊物件對伺服器端發送函數,表示僅傳送資料給伺服器

Example
let data = {...}
wo.deliver(data)
Parameters:
Name Type Description
data *

輸入發送函數之輸入資訊

(static) execute(func, inputopt)

Source:

Hapi通訊物件對伺服器端執行函數,表示傳送資料給伺服器,並請伺服器執行函數

Example
let func = 'NameOfFunction'
let input = {...}
wo.execute(func, input)
Parameters:
Name Type Attributes Default Description
func String

輸入執行函數之名稱字串

input * <optional>
null

輸入執行函數之輸入資訊

(static) onError(err)

Source:

Hapi監聽錯誤事件

Example
wo.on('error', function(err) {
    ...
})
Parameters:
Name Type Description
err *

接收錯誤訊息

(static) onOpen()

Source:

Hapi監聽開啟事件

Example
wo.on('open', function() {
    ...
})

(static) onOpenOnce()

Source:

Hapi監聽第一次開啟事件

Example
wo.on('openOnce', function() {
    ...
})