add udp example to readme

This commit is contained in:
funshine 2015-01-26 23:50:40 +08:00
parent afa741c192
commit b6185c88b5
2 changed files with 34 additions and 18 deletions

View File

@ -172,6 +172,20 @@ baudrate:9600
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n") .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
``` ```
####Or a simple http server
```lua
-- A simple http server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("<h1> Hello, NodeMcu.</h1>")
end)
conn:on("sent",function(conn) conn:close() end)
end)
```
####Connect to MQTT Broker ####Connect to MQTT Broker
```lua ```lua
@ -208,18 +222,18 @@ m:close();
``` ```
####Or a simple http server #### UDP client and server
```lua ```lua
-- A simple http server -- a udp server
srv=net.createServer(net.TCP) s=net.createServer(net.UDP)
srv:listen(80,function(conn) s:on("receive",function(s,c) print(c) end)
conn:on("receive",function(conn,payload) s:listen(5683)
print(payload)
conn:send("<h1> Hello, NodeMcu.</h1>") -- a udp client
end) cu=net.createConnection(net.UDP)
conn:on("sent",function(conn) conn:close() end) cu:on("receive",function(cu,c) print(c) end)
end) cu:connect(5683,"192.168.18.101")
cu:send("hello")
``` ```
####Do something shining ####Do something shining

View File

@ -25,15 +25,19 @@ ss=net.createServer(net.TCP) ss:listen(80,function(c) end)
s=net.createServer(net.TCP) s:listen(80,function(c) c:on("receive",function(s,c) print(c) end) end) s=net.createServer(net.TCP) s:listen(80,function(c) c:on("receive",function(s,c) print(c) end) end)
s=net.createServer(net.UDP) s:listen(5683,function(c) c:on("receive",function(s,c) print(c) end) end) s=net.createServer(net.UDP)
s:on("receive",function(s,c) print(c) end)
s:listen(5683)
su=net.createConnection(net.UDP)
su:on("receive",function(su,c) print(c) end)
su:connect(5683,"192.168.18.101")
su:send("hello")
mm=node.list() mm=node.list()
for k, v in pairs(mm) do print('file:'..k..' len:'..v) end for k, v in pairs(mm) do print('file:'..k..' len:'..v) end
for k,v in pairs(d) do print("n:"..k..", s:"..v) end for k,v in pairs(d) do print("n:"..k..", s:"..v) end
su=net.createConnection(net.UDP)
su:on("receive",function(su,c) print(c) end)
su:connect(5683,"192.168.0.66")
su:send("/v1/id")
gpio.mode(0,gpio.INT) gpio.trig(0,"down",function(l) print("level="..l) end) gpio.mode(0,gpio.INT) gpio.trig(0,"down",function(l) print("level="..l) end)
@ -57,8 +61,6 @@ su:send("hello world")
s=net.createServer(net.TCP) s:listen(8008,function(c) c:on("receive",function(s,c) print(c) pcall(loadstring(c)) end) end) s=net.createServer(net.TCP) s:listen(8008,function(c) c:on("receive",function(s,c) print(c) pcall(loadstring(c)) end) end)
s=net.createServer(net.UDP) s:listen(8888,function(c) c:on("receive",function(s,c) print(c) pcall(loadstring(c)) end) end)
s=net.createServer(net.TCP) s:listen(8008,function(c) con_std = c function s_output(str) if(con_std~=nil) then con_std:send(str) end end s=net.createServer(net.TCP) s:listen(8008,function(c) con_std = c function s_output(str) if(con_std~=nil) then con_std:send(str) end end
node.output(s_output, 0) c:on("receive",function(c,l) node.input(l) end) c:on("disconnection",function(c) con_std = nil node.output(nil) end) end) node.output(s_output, 0) c:on("receive",function(c,l) node.input(l) end) c:on("disconnection",function(c) con_std = nil node.output(nil) end) end)