new WConverhpServer(optopt) → {Object}
- Description:
建立Hapi伺服器
- Source:
Example
import WConverhpServer from 'w-converhp/dist/w-converhp-server.umd.js'
let opt = {
port: 8080,
apiName: 'api',
}
//new
let wo = new WConverhpServer(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(num) {
console.log(`Server[port:${opt.port}]: now clients: ${num}`)
})
wo.on('clientEnter', function(clientId, data) {
console.log(`Server[port:${opt.port}]: client enter: ${clientId}`)
//deliver
wo.deliver(clientId, `server deliver hi(${clientId})`)
})
wo.on('clientLeave', function(clientId, data) {
console.log(`Server[port:${opt.port}]: client leave: ${clientId}`)
})
wo.on('execute', function(func, input, pm) {
//console.log(`Server[port:${opt.port}]: execute`, func, input)
console.log(`Server[port:${opt.port}]: execute`, func)
try {
if (func === 'add') {
//save
if (_.get(input, 'p.d.u8a', null)) {
// fs.writeFileSync(input.p.d.name, Buffer.from(input.p.d.u8a))
// console.log('writeFileSync input.p.d.name', input.p.d.name)
}
let r = {
ab: input.p.a + input.p.b,
v: [11, 22.22, 'abc', { x: '21', y: 65.43, z: 'test中文' }],
file: {
name: 'zdata.b2',
u8a: new Uint8Array([66, 97, 115]),
//u8a: new Uint8Array(fs.readFileSync('C:\\Users\\Administrator\\Desktop\\z500mb.7z')),
},
}
pm.resolve(r)
}
else {
console.log('invalid func')
pm.reject('invalid func')
}
}
catch (err) {
console.log('execute error', err)
pm.reject('execute error')
}
})
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)
})
wo.on('handler', function(data) {
// console.log(`Server[port:${opt.port}]: handler`, 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, cb)
- Description:
Hapi通訊物件對全客戶端廣播函數
- Source:
Example
let data = {...}
wo.broadcast(data, cb)
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 輸入廣播函數之輸入資訊 |
cb |
function | 輸入進度函數 |
(static) deliver(clientId, data, cb)
- Description:
Hapi通訊物件對客戶端發送訊息函數
- Source:
Example
let clientId = '...'
let data = {...}
wo.deliver(clientId, data, cb)
Parameters:
Name | Type | Description |
---|---|---|
clientId |
String | 輸入識別用使用者主鍵字串 |
data |
* | 輸入發送函數之輸入資訊 |
cb |
function | 輸入進度函數 |
(static) onBroadcast(data)
- Description:
Hapi監聽客戶端廣播事件
- Source:
Example
wo.on('broadcast', function(data) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 傳入廣播訊息 |
(static) onClientChange()
- Description:
Hapi監聽客戶端變更(上下線)事件
- Source:
Example
wo.on('clientChange', function(num) {
...
})
(static) onClientEnter(clientId, data)
- Description:
Hapi監聽客戶端上線事件
- Source:
Example
wo.on('clientEnter', function(clientId, data) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
clientId |
String | 識別用使用者主鍵 |
data |
* | 使用者request訊息 |
(static) onClientLeave()
- Description:
Hapi監聽客戶端下線事件
- Source:
Example
wo.on('clientLeave', function(clientId, data) {
...
})
(static) onDeliver(data)
- Description:
Hapi監聽客戶端發送事件
- Source:
Example
wo.on('deliver', function(data) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
data |
* | 傳入發送訊息 |
(static) onError(err)
- Description:
Hapi監聽錯誤事件
- Source:
Example
wo.on('error', function(err) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
err |
* | 傳入錯誤訊息 |
(static) onExecute(func, input, callback, sendData)
- Description:
Hapi監聽客戶端執行事件
- Source:
Example
wo.on('execute', function(func, input, callback, sendData) {
...
})
Parameters:
Name | Type | Description |
---|---|---|
func |
String | 傳入執行函數名稱字串 |
input |
* | 傳入執行函數之輸入數據 |
callback |
function | 傳入執行函數之回調函數 |
sendData |
function | 傳入執行函數之強制回傳函數,提供傳送任意訊息給客戶端,供由服器多次回傳數據之用 |
(static) onOpen()
- Description:
Hapi監聽開啟事件
- Source:
Example
wo.on('open', function() {
...
})