Quick Apps — net.UDPSocket | FIBARO Dev

Quick Apps — net.UDPSocket

Class for handling the UDP.

UDPSocket class methods

net.UDPSocket(options)

UDPSocket class constructor.

Parameters

  • options - (optional) argument as a table containing options for the connection. Avaiable parameters:
    • broadcast - (optional) an argument of the type boolean that turns on the broadcast option
    • timeout - (optional) an argument of the type number with response timeout in milliseconds

Example

1function QuickApp:onInit()
2    self.udp = net.UDPSocket({ 
3        broadcast = true,
4        timeout = 5000
5    })
6end

UDPSocket:sendTo(data, ip, port, callbacks)

Method sends datagram to provided ip and port.

Parameters

  • data - an argument of the string type with data to be sent
  • ip - an argument of the string type with the IP address to establish the connection
  • port - an argument of the number type argument with the port to establish the connection
  • callbacks - (optional) an argument of the type table with callback actions, which will be executed in case of the relevant events described below

Feedback functions:

  • success() - the function will be triggered if the connection is correct
  • error(message) - The function will be triggered in case of an error when trying to connect, e.g. no connection. The parameter message (type string) passes the error message

Example

1function QuickApp:onInit()
2    self.udp = net.UDPSocket({ 
3        broadcast = true,
4        timeout = 1000
5    })
6
7    local payload = string.char(0x46,0x49,0x42,0x41,0x52,0x4f)
8
9    self.udp:sendTo(payload, '255.255.255.255', 44444, {
10        success = function()
11            self:receiveData()
12        end,
13        error = function(error)
14            print('Error:', error)
15        end 
16    })
17end

UDPSocket:receive(callbacks)

A method for reading data from a socket.

Parameters

  • callbacks - (optional) an argument of the type table with callback actions, which will be executed in case of the relevant events described below

Feedback functions:

  • success(data) - the function will be triggered if the data package is read correctly. The data is passed by the data argument, which is of string type
  • error(message) - The function will be triggered in case of an error when trying to read the data, e.g. timeout. The parameter message (type string) passes the error message
1function QuickApp:receiveData()
2    self.udp:receive({
3    success = function(data)
4        print(data)
5        self:receiveData() -- will read next datagram
6    end,
7    error = function(error)
8        self:debug("Error:", error)
9    end})
10end 

UDPSocket:close()

The method used to close the socket.