websocket - websocket Client#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view websocket demo examples

Example

local wsc = nil
if websocket then
    wsc = websocket.create(nil, "ws://echo.airtun.air32.cn/ws/echo")
    wsc:autoreconn(true, 3000) -- Automatic reconnection mechanism
    wsc:on(function(wsc, event, data)
        log.info("wsc", event, data)
        if event == "conack" then
            wsc:send((json.encode({action="echo", device_id=device_id})))
            sys.publish("wsc_conack")
        end
    end)
    wsc:connect()
    --sys.waitUntil("websocket_conack", 15000)
    while true do
        sys.wait(45000)
        if wsc:ready() then
            wsc:send((json.encode({action="echo", msg=os.date()})))
        end
    end
    wsc:close()
    wsc = nil
end

wsc:debug(onoff)#

Configure whether to open debug information

Parameters

Incoming Value Type

Explanation

boolean

Whether to turn on debug switch

Return Value

return value type

explanation

nil

No return value

Examples

None


websocket.create(adapter, url, keepalive)#

websocket Client Creation

Parameters

Incoming Value Type

Explanation

int

The adapter serial number can only be socket.ETH0, socket.STA, socket.AP. If it is not filled in, the platform’s own method will be selected, and then the last registered adapter will be selected.

string

connection strings, reference usage

int

Heartbeat interval, default 60 seconds. 2024.4.28 New

Return Value

return value type

explanation

userdata

Returns websocket client instance if successful, otherwise nil

Examples

-- Common TCP Link
wsc = websocket.create(nil,"ws://air32.cn/abc")
-- Encrypted TCP Link
wsc = websocket.create(nil,"wss://air32.cn/abc")

wsc:on(cb)#

Register websocket Callback

Parameters

Incoming Value Type

Explanation

function

cb websocket callback, parameters include websocket_client, event, data, payload

Return Value

return value type

explanation

nil

No return value

Examples

wsc:on(function(websocket_client, event, data, payload)
    -- Print various events
    log.info("websocket", "event", event, data, payload)
end)
--[[
event The value has:
    conack The connection to the server was successful, the websocket protocol header information has been received, and communication has been established.
    recv   Received information from the server, data, payload not nil
    sent   send The server has acknowledged receipt of the message sent by the function at the TCP protocol layer.
    disconnect Server connection disconnected

The sent/disconnect event is added on 2023.04.01
]]

wsc:connect()#

Connection Server

Parameters

None

Return Value

return value type

explanation

boolean

Returns true if the launch is successful, otherwise false

Examples

-- Start establishing a connection
wsc:connect()
-- This function only indicates that the launch is successful, and it is still necessary to determine whether the websocket is connected normally according to the ready function.

wsc:autoreconn(reconnect, reconnect_time)#

Automatic reconnect

Parameters

Incoming Value Type

Explanation

bool

Auto reconnect

int

Auto reconnect cycle unit ms default 3000ms

Return Value

None

Examples

wsc:autoreconn(true)

wsc:send(data, fin, opt)#

Publish message

Parameters

Incoming Value Type

Explanation

string

Data to be sent, required

int

Whether it is the last frame, the default is 1, that is, it is immediately set to the last frame, that is, single frame transmission

int

Opcode, default is string frame 0, optional 1

Return Value

return value type

explanation

bool

Returns true on success, otherwise false, or nil

Examples

-- Simple sending data
wsc:send("123")
-- Send data in segments, and finally use 1 (that is, the end of the FIN frame)
wsc:send("123", 0)
wsc:send("456", 0)
wsc:send("789", 1)

wsc:close()#

websocket Client shutdown (resources released after shutdown can no longer be used)

Parameters

None

Return Value

None

Examples

wsc:close()

wsc:ready()#

websocket Whether the client is ready

Parameters

None

Return Value

return value type

explanation

bool

Whether the client is ready

Examples

local stat = wsc:ready()

wsc:headers(headers)#

Set additional headers

Parameters

Incoming Value Type

Explanation

table/string

It can be a table or a string.

Return Value

return value type

explanation

bool

Whether the client is ready

Examples

-- table Form
wsc:headers({
    Auth="Basic ABCDEFGG"
})
-- string form
wsc:headers("Auth: Basic ABCDERG\r\n")