Extract and hoist net receive callbacks

This is done to avoid the accidental upval binding
This commit is contained in:
Marcel Stör 2016-11-27 21:42:10 +01:00
parent c54bc05ba6
commit 1032e9dd90
1 changed files with 38 additions and 29 deletions

View File

@ -135,15 +135,21 @@ Listen on port from IP address.
#### Example #### Example
```lua ```lua
-- 30s time out for a inactive client
sv = net.createServer(net.TCP, 30)
-- server listens on 80, if data received, print data to console and send "hello world" back to caller -- server listens on 80, if data received, print data to console and send "hello world" back to caller
sv:listen(80, function(c) -- 30s time out for a inactive client
c:on("receive", function(c, pl) sv = net.createServer(net.TCP, 30)
print(pl)
function receiver(sck, data)
print(data)
sck:close()
end
if sv then
sv:listen(80, function(conn)
conn:on("receive", receiver)
conn:send("hello world")
end) end)
c:send("hello world") end
end)
``` ```
#### See also #### See also
@ -303,32 +309,35 @@ Multiple consecutive `send()` calls aren't guaranteed to work (and often don't)
#### Example #### Example
```lua ```lua
srv = net.createServer(net.TCP) srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
conn:on("receive", function(sck, req)
local response = {}
-- if you're sending back HTML over HTTP you'll want something like this instead function receiver(sck, data)
-- local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"} local response = {}
response[#response + 1] = "lots of data" -- if you're sending back HTML over HTTP you'll want something like this instead
response[#response + 1] = "even more data" -- local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"}
response[#response + 1] = "e.g. content read from a file"
response[#response + 1] = "lots of data"
-- sends and removes the first element from the 'response' table response[#response + 1] = "even more data"
local function send(sk) response[#response + 1] = "e.g. content read from a file"
if #response > 0
then sk:send(table.remove(response, 1)) -- sends and removes the first element from the 'response' table
else local function send(localSocket)
sk:close() if #response > 0
response = nil then localSocket:send(table.remove(response, 1))
end else
localSocket:close()
response = nil
end end
end
-- triggers the send() function again once the first chunk of data was sent -- triggers the send() function again once the first chunk of data was sent
sck:on("sent", send) sck:on("sent", send)
send(sck) send(sck)
end) end
srv:listen(80, function(conn)
conn:on("receive", receiver)
end) end)
``` ```
If you do not or can not keep all the data you send back in memory at one time (remember that `response` is an aggregation) you may use explicit callbacks instead of building up a table like so: If you do not or can not keep all the data you send back in memory at one time (remember that `response` is an aggregation) you may use explicit callbacks instead of building up a table like so: