3 local socket = require "socket"
5 local uloop = require("uloop")
8 local udp = socket.udp()
10 udp:setsockname('*', 8080)
12 -- timer example 1 (will run repeatedly)
15 print("1000 ms timer run");
18 timer = uloop.timer(t)
21 -- timer example 2 (will run once)
22 uloop.timer(function() print("2000 ms timer run"); end, 2000)
24 -- timer example 3 (will never run)
25 uloop.timer(function() print("3000 ms timer run"); end, 3000):cancel()
27 -- periodic interval timer
29 intv = uloop.interval(function()
30 print(string.format("Interval expiration #%d - %dms until next expiration",
31 intv:expirations(), intv:remaining()))
33 -- after 5 expirations, lower interval to 500ms
34 if intv:expirations() >= 5 then
38 -- cancel after 10 expirations
39 if intv:expirations() >= 10 then
46 print("Process 1 completed")
51 print("Process 2 completed")
57 uloop.process("uloop_pid_test.sh", {"foo", "bar"}, {"PROCESS=1"}, p1)
62 uloop.process("uloop_pid_test.sh", {"foo", "bar"}, {"PROCESS=2"}, p2)
66 -- Keep udp_ev reference, events will be gc'd, even if the callback is still referenced
67 -- .delete will manually untrack.
68 udp_ev = uloop.fd_add(udp, function(ufd, events)
69 local words, msg_or_ip, port_or_nil = ufd:receivefrom()
70 print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
71 if words == "Stop!" then
74 end, uloop.ULOOP_READ)
77 udp_send_timer = uloop.timer(
79 local s = socket.udp()
83 udp_send_timer:cancel()
86 udp_send_timer:set(1000)
88 print('Send UDP packet to 127.0.0.1:8080 :'..words)
89 s:sendto(words, '127.0.0.1', 8080)
92 udp_count = udp_count + 1