new WConversiServer(optopt) → {Object}
- Description:
建立SocketIO伺服器
- Source:
Example
import WConversiServer from 'w-conversi/dist/w-conversi-server.umd.js'
let opt = {
port: 8080,
}
//new
let wo = new WConversiServer(opt)
wo.on('open', function() {
console.log(`Server[port:${opt.port}]: open`)
//broadcast
// let n = 0
// setInterval(() => {
// n += 1
// let o = {
// text: `server broadcast hi(${n})`,
// data: new Uint8Array([66, 97, 115]), //support Uint8Array data
// }
// wo.broadcast(o, function (prog) {
// console.log('broadcast prog', prog)
// })
// }, 1000)
})
wo.on('error', function(err) {
console.log(`Server[port:${opt.port}]: error`, err)
})
wo.on('clientChange', function(clients) {
console.log(`Server[port:${opt.port}]: now clients: ${clients.length}`)
})
wo.on('execute', function(func, input, callback) {
console.log(`Server[port:${opt.port}]: execute`, func, input)
if (func === 'add') {
let r = input.p1 + input.p2
callback(r)
}
})
wo.on('broadcast', function(data) {
console.log(`Server[port:${opt.port}]: broadcast`, data)
})
wo.on('deliver', function(data) {
console.log(`Server[port:${opt.port}]: deliver`, data)
})
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳通訊物件,可監聽事件open、error、clientChange、execute、broadcast、deliver,可使用函數broadcast
- Type
- Object
Methods
(static) broadcast(data)
- Description:
SocketIO通訊物件對客戶端廣播函數
- Source:
Example
let data = {...}
wo.broadcast(data)
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 輸入廣播函數之輸入資訊 |
(static) onBroadcast(data)
- Description:
SocketIO監聽客戶端廣播事件
- Source:
Example
wo.on('broadcast', function(data) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 傳入廣播訊息 |
(static) onClientChange()
- Description:
SocketIO監聽客戶端變更(上下線)事件
- Source:
Example
wo.on('clientChange', function(clients) {
...
})
(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) onExecute(func, input, callback, sendData)
- Description:
SocketIO監聽客戶端執行事件
- Source:
Example
wo.on('execute', function(func, input, callback, sendData) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
func |
String | 傳入執行函數名稱字串 |
input |
* | 傳入執行函數之輸入數據 |
callback |
function | 傳入執行函數之回調函數 |
sendData |
function | 傳入執行函數之強制回傳函數,提供傳送任意訊息給客戶端,供由服器多次回傳數據之用 |
(static) onOpen()
- Description:
SocketIO監聽開啟事件
- Source:
Example
wo.on('open', function() {
...
})