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)
67 uloop.signal(function(signo)
68 print(string.format("Terminating on SIGINT (#%d)!", signo))
70 -- end uloop to terminate program
75 sig = uloop.signal(function(signo)
76 print(string.format("Got SIGUSR2 (#%d)!", signo))
78 -- remove signal handler, next SIGUSR2 will terminate program
82 -- Keep udp_ev reference, events will be gc'd, even if the callback is still referenced
83 -- .delete will manually untrack.
84 udp_ev = uloop.fd_add(udp, function(ufd, events)
85 local words, msg_or_ip, port_or_nil = ufd:receivefrom()
86 print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
87 if words == "Stop!" then
90 end, uloop.ULOOP_READ)
93 udp_send_timer = uloop.timer(
95 local s = socket.udp()
99 udp_send_timer:cancel()
102 udp_send_timer:set(1000)
104 print('Send UDP packet to 127.0.0.1:8080 :'..words)
105 s:sendto(words, '127.0.0.1', 8080)
108 udp_count = udp_count + 1