As the title says... Chat on...

User avatar
By cwilt
#22510 I am experimenting with receiving data from a weather API but the amount of data received exceeds buffer several times over. Of course this data could be written to a file and parsed but the flash storage would not live long. I just need to be pointed in the right direction to handle this overflow.

Sample receive function:
Code: Select allconn:on("receive", function(conn,payload)
print(string.len(payload))
end)


The output of this code is:
1460
1460
114

Thanks in advance for any help.
User avatar
By TerryE
#22850 The http protocol includes size information, so the easiest method is to use the on("receive") callback to append the incoming records to an input array using
Code: Select allbuf[#buf+1] = payload
len = len + #payload
then process the payload buf array once the full content has been received
User avatar
By kolban
#22887 I think it would be useful to describe to us what you want to happen. If the server sends "X" bytes of data, your receive callback will be called multiple times in units of 1460 bytes ... however it will get to see all the data.

Let us assume that you were able to receive the data in one unit ... what would you do with it? You say that it is JSON data ... well that is a String representation of JavaScript objects ... but it is still string data. So in principle, as the other poster said, you could allocate enough buffer space for your whole message and then append the "parts" to that buffer on each chunk of 1460 bytes until you have received the whole message. How will you know that the end of the message has been reached? Does the sender disconnect at that point?