switch from typeof to the more portable __typeof__
[project/libubox.git] / examples / uloop-example.lua
index 2da6ebd..9b0684e 100755 (executable)
@@ -1,8 +1,14 @@
 #!/usr/bin/env lua
 
+local socket = require "socket"
+
 local uloop = require("uloop")
 uloop.init()
 
+local udp = socket.udp()
+udp:settimeout(0)
+udp:setsockname('*', 8080)
+
 -- timer example 1
 local timer
 function t()
@@ -40,5 +46,33 @@ uloop.timer(
        end, 2000
 )
 
+udp_ev = uloop.fd_add(udp, function(ufd, events)
+       local words, msg_or_ip, port_or_nil = ufd:receivefrom()
+       print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
+       if words == "Stop!" then
+               udp_ev:delete()
+       end
+end, uloop.ULOOP_READ)
+
+udp_count = 0
+udp_send_timer = uloop.timer(
+       function()
+               local s = socket.udp()
+               local words
+               if udp_count > 3 then
+                       words = "Stop!"
+                       udp_send_timer:cancel()
+               else
+                       words = 'Hello!'
+                       udp_send_timer:set(1000)
+               end
+               print('Send UDP packet to 127.0.0.1:8080 :'..words)
+               s:sendto(words, '127.0.0.1', 8080)
+               s:close()
+
+               udp_count = udp_count + 1
+       end, 3000
+)
+
 uloop.run()