Replies: 1 comment
-
Thank you, I already wrote similar code
…On Tue, Jun 16, 2020, 23:39 hst-m ***@***.***> wrote:
I saw @zdm <https://github.com/zdm> ask related question about sending
out data with res.end() vs res.tryEnd()
In my case I have similar situation where I have a long string of data in
Json format to send out, usually around 400k bytes. Technically it works
with res.end() but if you use res.tryEnd() it breaks it down into chunks
which I believe is preferred
I coded a function that can be re-used for streaming out a long string
response, taken from the VideoStreamer.js example but simplified for the
case of pre-loaded string, in case anyone needs something similar. This is
better for Discussions
<uNetworking/uWebSockets#1048> page but that is
not out yet so can close this issue, or if anyone thinks this code is not
good I will update it
// example function sends out 1MB long string, breaking down into chunks as needed
uws.App().any('/*', (res, req) => {
const longString = 'x'.repeat(1000000) // 1000 kilobyte long string
resStreamString(res, longString)})
const resStreamString = (res, str) => {
const totalSize = str.length // only use length if using simple/ASCII text, otherwise use: totalSize = new TextEncoder().encode(str).length
const [ok, done] = res.tryEnd(str, totalSize)
if (ok) return
res.data = str
res.onWritable(offset => {
const [ok, done] = res.tryEnd(res.data.slice(offset), totalSize)
return ok
})
res.onAborted(() => res.aborted = true) // onAborted required for async responses}
if you add console logs for tryEnd for that example you get:
tryEnd(data, totalSize:1000000): ok: false, done: false
tryEnd(data.slice(110916), totalSize:1000000): ok: false, done: false
tryEnd(data.slice(267136), totalSize:1000000): ok: false, done: false
tryEnd(data.slice(462776), totalSize:1000000): ok: false, done: false
tryEnd(data.slice(700756), totalSize:1000000): ok: false, done: false
tryEnd(data.slice(982536), totalSize:1000000): ok: true, done: true
looks like anything over 110KB gets broken down into chunks so anything
over that size you would want to use this for. I just coded this today
because I need to use this for my API
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/uNetworking/uWebSockets.js/issues/330>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAH2MSCL6D6BDRQAEGVMZHLRW7J7TANCNFSM4N763BCQ>
.
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Beta Was this translation helpful? Give feedback.
All reactions