`sck:send(data, fnA)` is functionally equivalent to `sck:send(data) sck:on("sent", fnA)`.
#### Parameters
#### Parameters
-`string` data in string which will be sent to server
-`string` data in string which will be sent to server
...
@@ -212,7 +214,51 @@ Sends data to server.
...
@@ -212,7 +214,51 @@ Sends data to server.
#### Note
#### Note
Multiple consecutive `send()` calls aren't guaranteed to work (and often don't) as network requests are treated as separate tasks by the SDK. Instead, subscribe to the "sent" event on the socket and send additional data (or close) in that callback. See [#730](https://github.com/nodemcu/nodemcu-firmware/issues/730#issuecomment-154241161) for an example and explanation.
Multiple consecutive `send()` calls aren't guaranteed to work (and often don't) as network requests are treated as separate tasks by the SDK. Instead, subscribe to the "sent" event on the socket and send additional data (or close) in that callback. See [#730](https://github.com/nodemcu/nodemcu-firmware/issues/730#issuecomment-154241161) for details.
#### Example
```lua
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(sck,req)
localresponse={}
-- if you're sending back HTML over HTTP you'll want something like this instead
-- local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"}
response[#response+1]="lots of data"
response[#response+1]="even more data"
response[#response+1]="e.g. content read from a file"
-- sends and removes the first element from the 'response' table
localfunctionsend()
if#response>0
thensck:send(table.remove(response,1))
else
sck:close()
end
end
-- triggers the send() function again once the first chunk of data was sent
sck:on("sent",send)
send()
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:
```lua
sck:send(header,function()
localdata1="some large chunk of dynamically loaded data"