WConversiClient

WConversiClient

new WConversiClient(opt) → {Object}

Description:
  • 建立SocketIO使用者(Node.js與Browser)端物件

Source:
Example
import WConversiClient from 'w-conversi/dist/w-conversi-client.umd.js'

let opt = {
    url: 'http://localhost:8080',
    token: '*',
}

//new
let wo = new WConversiClient(opt)

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

    //execute
    wo.execute('add', { p1: 1, p2: 2 },
        function (prog) {
            console.log('client nodejs: execute prog=', prog)
        })
        .then(function(r) {
            console.log('client nodejs: execute: add=', r)
        })

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

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

})
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) { //伺服器目前無法針對client直接deliver
//     console.log('client nodejs: deliver=', data)
// })
Parameters:
Name Type Description
opt Object

輸入設定參數物件

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

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

token String <optional>
'*'

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

strSplitLength Integer <optional>
1000000

輸入傳輸封包長度整數,預設為1000000

ioSettings Object <optional>
{}

輸入SocketIO初始化設定物件,預設為{}

Returns:

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

Type
Object

Methods

(static) broadcast(data)

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

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

輸入廣播函數之輸入資訊

(static) deliver(data)

Description:
  • SocketIO通訊物件對伺服器端交付函數,表示僅傳送資料給伺服器

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

輸入廣播函數之輸入資訊

(static) execute(func, inputopt)

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

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

輸入執行函數之名稱字串

input * <optional>
null

輸入執行函數之輸入資訊

(static) onBroadcast(data)

Description:
  • SocketIO監聽伺服器端廣播事件

Source:
Example
wo.on('broadcast', function(data) {
    ...
})
Parameters:
Name Type Description
data *

傳入廣播訊息

(static) onClose()

Description:
  • SocketIO監聽關閉事件

Source:
Example
wo.on('close', function() {
    ...
})

(static) onDeliver(data)

Description:
  • SocketIO監聽伺服器端交付事件

Source:
Example
wo.on('deliver', function(data) {
    ...
})
Parameters:
Name Type Description
data *

傳入交付訊息

(static) onError(err)

Description:
  • SocketIO錯誤事件

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

接收錯誤訊息

(static) onOpen()

Description:
  • SocketIO監聽開啟事件

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

(static) onOpenOnce()

Description:
  • SocketIO監聽第一次開啟事件

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

(static) onReconn()

Description:
  • SocketIO監聽重連事件

Source:
Example
wo.on('reconn', function() {
    ...
})