Left here for archival purposes.

User avatar
By Markus Gritsch
#5884 So the question is, was this change that the "sent" callback function is called immediately after the first conn:send() on purpose?

What should the semantics of "sent" be: Should we perform subsequent calls to conn:send() in the "sent" callback, keep track of the progress and send conn:close() at the end?
User avatar
By papagenic
#6282 the conn:on('sent'..) function will be called repeatedly every time a chunk of data has been sent. So it is possble to do multiple send using a control structure like this ( using a global variable)
Code: Select all ind=1
 conn:on("sent",function(conn) 
    if ind==1 then
      conn:send("hello1\n")
    elseif ind==2 then
      conn:send("hello 2\n")
    else
      conn:close()
    end
    ind=ind+1
  end)

Not very nice, but still no good to send data obtained in a loop like:
Code: Select allfile.open('file.txt', 'r')
while true do
   local line = file.readline()
   if (line == nil) then break end
   conn:send(line)
 end

what would be the recommenday way to handle this case?
really wonder about the reason that triggered such a drastic change in the send model
User avatar
By Markus Gritsch
#6326 I don't know anything of the background on the change in the sent model, but your approach of sending a file should probably look something like this (taken from viewtopic.php?f=19&t=702&start=30#p4467):
Code: Select all         local isopen=false

         conn:on("sent",function(conn)
            if not isopen then
               isopen = true
               file.open("bigfile.html", "r")
            end
            local line = file.readline()
            if line then
               conn:send(line)
            else
               file.close()
               conn:close()
               conn=nil
            end
         end)