new WConverwsClient(opt) → {Object}
- Description:
建立WebSocket使用者(Node.js與Browser)端物件
- Source:
Example
import WConverwsClient from 'w-converws/dist/w-converws-client.umd.js'
let opt = {
url: 'ws://localhost:8080',
token: '*',
}
//new
let wo = new WConverwsClient(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
|
Returns:
回傳通訊物件,可監聽事件open、openOnce、close、error、reconn、broadcast、deliver,可使用函數execute、broadcast、deliver
- Type
- Object
Methods
(static) broadcast(data)
- Description:
WebSocket通訊物件對伺服器端廣播函數,表示傳送資料給伺服器,還需轉送其他客戶端
- Source:
Example
let data = {...}
wo.broadcast(data)
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 輸入廣播函數之輸入資訊 |
(static) deliver(data)
- Description:
WebSocket通訊物件對伺服器端交付函數,表示僅傳送資料給伺服器
- Source:
Example
let data = {...}
wo.deliver(data)
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 輸入廣播函數之輸入資訊 |
(static) execute(func, inputopt)
- Description:
WebSocket通訊物件對伺服器端執行函數,表示傳送資料給伺服器,並請伺服器執行函數
- 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:
WebSocket監聽伺服器端廣播事件
- Source:
Example
wo.on('broadcast', function(data) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 傳入廣播訊息 |
(static) onClose()
- Description:
WebSocket監聽關閉事件
- Source:
Example
wo.on('close', function() {
...
})
(static) onDeliver(data)
- Description:
WebSocket監聽伺服器端交付事件
- Source:
Example
wo.on('deliver', function(data) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 傳入交付訊息 |
(static) onError(err)
- Description:
WebSocket錯誤事件
- Source:
Example
wo.on('error', function(err) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
err |
* | 接收錯誤訊息 |
(static) onOpen()
- Description:
WebSocket監聽開啟事件
- Source:
Example
wo.on('open', function() {
...
})
(static) onOpenOnce()
- Description:
WebSocket監聽第一次開啟事件
- Source:
Example
wo.on('openOnce', function() {
...
})
(static) onReconn()
- Description:
WebSocket監聽重連事件
- Source:
Example
wo.on('reconn', function() {
...
})