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 typebooleanthat turns on the broadcast optiontimeout- (optional) an argument of the typenumberwith 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 thestringtype with data to be sentip- an argument of thestringtype with the IP address to establish the connectionport- an argument of thenumbertype argument with the port to establish the connectioncallbacks- (optional) an argument of the typetablewith 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 correcterror(message)- The function will be triggered in case of an error when trying to connect, e.g. no connection. The parametermessage(typestring) 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 typetablewith 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 thedataargument, which is ofstringtypeerror(message)- The function will be triggered in case of an error when trying to read the data, e.g. timeout. The parametermessage(typestring) 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.