Global

Methods

WComorSocketioClient(opt) → {Promise}

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

Source:
Example
import WComorSocketioClient from 'w-comor-socketio/dist/w-comor-socketio-client.umd.js'

//opt
let opt = {
    url: 'http://localhost:8080',
    token: '*',
    open: function() {
        console.log('client nodejs: open')
    },
    close: function() {
        console.log('client nodejs: close')
    },
    error: function(err) {
        console.log('client nodejs: error:', err)
    },
    reconn: function() {
        console.log('client nodejs: reconn')
    },
}

//WComorSocketioClient
new WComorSocketioClient(opt)
    .then(function(wo) {
        console.log('client: funcs: ', wo)

        function core(ps) {
            wo.group.plus(ps)
                .then(function(r) {
                    console.log('client: plus(' + JSON.stringify(ps) + ')=' + r)
                })
                .catch(function(err) {
                    console.log('client: plus: catch: ', err)
                })
            wo.group.div(ps)
                .then(function(r) {
                    console.log('client: div(' + JSON.stringify(ps) + ')=' + r)
                })
                .catch(function(err) {
                    console.log('client: div: catch: ', err)
                })
            wo.add(ps)
                .then(function(r) {
                    console.log(`client: add(${JSON.stringify(ps)})=${r}`)
                })
                .catch(function(err) {
                    console.log('client: add: catch: ', err)
                })
            wo.minu(ps)
                .then(function(r) {
                    console.log(`client: minu(${JSON.stringify(ps)})=${r}`)
                })
                .catch(function(err) {
                    console.log('client: minu: catch: ', err)
                })
        }

        let i = 100
        setInterval(function() {
            i += 1
            core({
                p1: i,
                p2: 10,
            })
        }, 1000)

    })
    .catch(function(err) {
        console.log('client: catch', err)
    })
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,預設為'*'

ioSettings Object <optional>
{}

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

open function

輸入監聽open函數

close function

輸入監聽close函數

error function

輸入監聽error函數

reconn function

輸入監聽reconn函數

Returns:

回傳Promise,resolve為映射伺服器端可用函數之物件,各函數輸入皆為單一物件,各函數回傳皆為Promise,用resolve與reject處理回傳結果

Type
Promise

WComorSocketioServer(opt)

Description:
  • 建立socket.io伺服器

Source:
Example
import WComorSocketioServer from 'w-comor-socketio/dist/w-comor-socketio-server.umd.js'

function random(min, max) {
    return Math.floor(Math.random() * max) + min
}

let opt = {
    port: 8080,
    authenticate: function(token) {
        //使用token驗證使用者身份
        return new Promise(function(resolve, reject) {
            setTimeout(function() {
                resolve(true)
            }, 1000)
        })
    },
    filterFuncs: function(token, funcs) {
        //使用token驗證使用者身份與過濾可用funcs
        return new Promise(function(resolve, reject) {
            funcs = funcs.filter(function(v) {
                return v.indexOf('Hide') < 0
            })
            resolve(funcs)
        })
    },
    onClientChange: function(clients, opt) {
        console.log(`Server[port:${opt.port}] now clients: ${clients.length}`)
    },
    funcs: {
        'group.plus': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 * p2)
                }, random(100, 3000))
            })
        },
        'group.div': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 / p2)
                }, random(100, 3000))
            })
        },
        'add': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 + p2)
                }, random(100, 3000))
            })
        },
        'addHide': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 + p2)
                }, random(100, 3000))
            })
        },
        'minu': function({ p1, p2 }) {
            return new Promise(function(resolve, reject) {
                setTimeout(function() {
                    resolve(p1 - p2)
                }, random(100, 3000))
            })
        },
    },
}

new WComorSocketioServer(opt)
Parameters:
Name Type Description
opt Object

輸入設定參數物件

Properties
Name Type Attributes Default Description
serverHapi Object <optional>
{}

輸入hapi伺服器物件,若提供,本服務將自動加入api至route。使用外部hapi伺服器時,需開啟跨域功能,或是使用nginx反向代理轉入api請求

port Integer <optional>
8080

輸入hapi與socket.io伺服器所在port,為hapi與socket.io共用,預設8080

pathPolling String <optional>

輸入socket.io伺服器無法使用WebSocket連線自動降級成為輪詢(polling)所在子目錄字串,預設undefined,代表使用'/socket.io'

authenticate function

輸入使用者身份認證函數,供伺服器端驗證之用,函數會傳入使用者端連線之token參數,回傳為Promise,resolve(true)為驗證通過,resolve(false)為驗證不通過

funcs Object <optional>
{}

輸入伺服器端供使用者端呼叫之函數物件,各key為函數名稱,對應value為函數本體。各函數之輸入需為單一物件,而各函數回傳皆為Promise,可通過resolve與reject回傳結果,預設{}

routes Array <optional>
[]

輸入伺服器額外掛載routes陣列,預設[]