#
hs.socket.udp
Talk to custom protocols using asynchronous UDP sockets.
For TCP sockets see hs.socket
.
You can do a lot of neat trivial and non-trivial things with these. A simple ping ponger:
function ping(data, addr)
print(data)
addr = hs.socket.parseAddress(addr)
hs.timer.doAfter(1, function()
client:send("ping", addr.host, addr.port)
end)
end
function pong(data, addr)
print(data)
addr = hs.socket.parseAddress(addr)
hs.timer.doAfter(1, function()
server:send("pong", addr.host, addr.port)
end)
end
server = hs.socket.udp.server(9001, pong):receive()
client = hs.socket.udp.new(ping):send("ping", "localhost", 9001):receive()
Resulting in the following endless exchange:
20:26:56 LuaSkin: (secondary thread): Data written to UDP socket
LuaSkin: (secondary thread): Data read from UDP socket
ping
20:26:57 LuaSkin: (secondary thread): Data written to UDP socket
LuaSkin: (secondary thread): Data read from UDP socket
pong
20:26:58 LuaSkin: (secondary thread): Data written to UDP socket
LuaSkin: (secondary thread): Data read from UDP socket
ping
20:26:59 LuaSkin: (secondary thread): Data written to UDP socket
LuaSkin: (secondary thread): Data read from UDP socket
pong
...
You can do some silly things with a callback factory and enabling broadcasting:
local function callbackMaker(name)
local fun = function(data, addr)
addr = hs.socket.parseAddress(addr)
print(name.." received data:\n"..data.."\nfrom host: "..addr.host.." port: "..addr.port)
end
return fun
end
local listeners = {}
local port = 9001
for i=1,3 do
table.insert(listeners, hs.socket.udp.new(callbackMaker("listener "..i)):reusePort():listen(port):receive())
end
broadcaster = hs.socket.udp.new():broadcast()
broadcaster:send("hello!", "255.255.255.255", port)
Since neither IPv4 nor IPv6 have been disabled, the broadcast is received on both protocols ('dual-stack' IPv6 addresses shown):
listener 2 received data:
hello!
from host: ::ffff:192.168.0.3 port: 53057
listener 1 received data:
hello!
from host: ::ffff:192.168.0.3 port: 53057
listener 3 received data:
hello!
from host: ::ffff:192.168.0.3 port: 53057
listener 1 received data:
hello!
from host: 192.168.0.3 port: 53057
listener 3 received data:
hello!
from host: 192.168.0.3 port: 53057
listener 2 received data:
hello!
from host: 192.168.0.3 port: 53057
#
API Overview
Variables - Configurable values
timeout
Functions - API calls offered directly by the extension
parseAddress
Constructors - API calls which return an object, typically one that offers API methods
new server
Methods - API calls which can only be made on an object returned by a constructor
broadcast close closed connect connected enableIPv info listen pause preferIPv read readOne receive receiveOne reusePort send setBufferSize setCallback setTimeout write
#
API Documentation
#
Variables
#
timeout
#
Functions
#
parseAddress
#
Constructors
#
new
#
server
#
Methods
#
broadcast
#
close
#
closed
#
connect
#
connected
#
enableIPv
#
info
#
listen
#
pause
#
preferIPv
#
read
#
readOne
#
receive
#
receiveOne
#
reusePort
#
send
#
setBufferSize
#
setCallback
| | |
| --------------------------------------------|-------------------------------------------------------------------------------------|
| Signature | hs.socket.udp:setCallback([fn]) -> self
|
| Type | Method |
| Description | Sets the read callback for the socket. |
| Parameters |
fn
- An optional callback function to process data read from the socket.nil
or no argument clears the callback. The callback receives 2 parameters:data
- The data read from the socket as a string.sockaddr
- The sending address as a binary socket address structure. See .parseAddress
- The
object.hs.socket.udp
- A callback must be set in order to read data from the socket.