Closed liweijian closed 4 years ago
You should return a response constructed from a Lwt_stream.t
:
Then it's just a question of constructing an Lwt_stream that returns chunks with a delay.
Let me know if that isn't clear enough and I will whip up a demo
@rgrinberg ,thank you for your reply.
I have tried to read the documents of both cohttp_lwt_body and Lwt_stream API, this is the first time I use Lwt library.
I tried to write some examples of Lwt_stream
, but there're errrors, for example:
let delay_resp req =
let ret_str = "hello" in
respond (`Stream (Lwt_stream.of_string ret_str))
+1 for adding an example that uses streaming
Version 0.18.0 added support for streaming responses.
Example with cohttp + Lwt_stream https://github.com/rgrinberg/opium/blob/6ce8a21223b0d74a77d5ea0160d86f49db94ace1/examples/hello_world.ml#L13-L29
Note that the current master branch has switched to httpaf. Example with httpaf + streams
Normally, we wrote code to response as soon as possible, for example:
In some situations we need to simulate slow internet connection by delay the response, I was wondering can we do that in opium?