Global

Methods

WWebsocketClient(opt) → {Promise}

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

Source:
Example
import WSC from 'w-websocket-client/dist/w-websocket-client.umd.js'

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

//wsc
let wsc = null
new WSC(opt)
    .then(function(w){

        //save
        wsc = w

        //send
        let data = 'abc'
        wsc.send(data)

    })
Parameters:
Name Type Description
opt Object

輸入設定參數物件

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

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

token String <optional>
'*'

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

open function

輸入監聽open函數

close function

輸入監聽close函數

message function

輸入監聽message函數

error function

輸入監聽error函數

Returns:

回傳Promise,resolve為回傳連接成功的Websocket物件,reject回傳為無法連接

Type
Promise