diff options
author | Xiongfei Guo <xfguo@credosemi.com> | 2014-06-20 14:31:18 +0400 |
---|---|---|
committer | John Crispin <blogic@openwrt.org> | 2014-06-24 17:30:30 +0400 |
commit | 79b56268b46ea2eaf7f79af7a64c57e2be37636a (patch) | |
tree | 2319f03ed7d0b045e998710454c17013325a8135 /examples | |
parent | 9565bf86ae48c47c744467e30b2219cf9cd70fbf (diff) |
Added fd_add method for uloop lua binding.
Use uloop.fd_add like this:
local socket = require "socket"
udp = socket.udp()
uloop.fd_add(
udp, -- socket
function( -- callback function
ufd, -- socket object when register the fd
events -- uloop events. eg. uloop.ULOOP_READ .
)
local words, msg_or_ip, port_or_nil = ufd:receivefrom()
print('Recv UDP packet from '..msg_or_ip..':'..port_or_nil..' : '..words)
end,
uloop.ULOOP_READ -- event you want to listen
)
The `examples/uloop-example.lua` show an example of this work.
Signed-off-by: Xiongfei(Alex) Guo <xfguo@credosemi.com>
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/uloop-example.lua | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/examples/uloop-example.lua b/examples/uloop-example.lua index 2da6ebd..ba34ec5 100755 --- a/examples/uloop-example.lua +++ b/examples/uloop-example.lua @@ -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,22 @@ uloop.timer( end, 2000 ) +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) +end, uloop.ULOOP_READ) + +udp_send_timer = uloop.timer( + function() + local s = socket.udp() + local words = 'Hello!' + print('Send UDP packet to 127.0.0.1:8080 :'..words) + s:sendto(words, '127.0.0.1', 8080) + s:close() + + udp_send_timer:set(1000) + end, 3000 +) + uloop.run() |