examples/lua: attempt to highlight some traps
Ran into some issues with my fd event being garbage collected. As I never wanted to call :delete, I had seen no reason to keep the returned object, as my callback and upvalues were still valid. Signed-off-by: Karl Palsson <karlp@etactica.com>
This commit is contained in:
parent
53b9a2123f
commit
f4e9bf73ac
1 changed files with 5 additions and 3 deletions
|
@ -9,7 +9,7 @@ local udp = socket.udp()
|
||||||
udp:settimeout(0)
|
udp:settimeout(0)
|
||||||
udp:setsockname('*', 8080)
|
udp:setsockname('*', 8080)
|
||||||
|
|
||||||
-- timer example 1
|
-- timer example 1 (will run repeatedly)
|
||||||
local timer
|
local timer
|
||||||
function t()
|
function t()
|
||||||
print("1000 ms timer run");
|
print("1000 ms timer run");
|
||||||
|
@ -18,10 +18,10 @@ end
|
||||||
timer = uloop.timer(t)
|
timer = uloop.timer(t)
|
||||||
timer:set(1000)
|
timer:set(1000)
|
||||||
|
|
||||||
-- timer example 2
|
-- timer example 2 (will run once)
|
||||||
uloop.timer(function() print("2000 ms timer run"); end, 2000)
|
uloop.timer(function() print("2000 ms timer run"); end, 2000)
|
||||||
|
|
||||||
-- timer example 3
|
-- timer example 3 (will never run)
|
||||||
uloop.timer(function() print("3000 ms timer run"); end, 3000):cancel()
|
uloop.timer(function() print("3000 ms timer run"); end, 3000):cancel()
|
||||||
|
|
||||||
-- process
|
-- process
|
||||||
|
@ -46,6 +46,8 @@ uloop.timer(
|
||||||
end, 2000
|
end, 2000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
-- Keep udp_ev reference, events will be gc'd, even if the callback is still referenced
|
||||||
|
-- .delete will manually untrack.
|
||||||
udp_ev = uloop.fd_add(udp, function(ufd, events)
|
udp_ev = uloop.fd_add(udp, function(ufd, events)
|
||||||
local words, msg_or_ip, port_or_nil = ufd:receivefrom()
|
local words, msg_or_ip, port_or_nil = ufd:receivefrom()
|
||||||
print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
|
print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
|
||||||
|
|
Loading…
Reference in a new issue