File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed
Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -82,8 +82,27 @@ let print_param =
8282 put "/hello/:name" (fun req ->
8383 `String ("Hello " ^ param req "name") |> respond')
8484
85+ let streaming =
86+ let open Lwt.Infix in
87+ get "/hello/stream" (fun _req ->
88+ (* [create_stream] returns a push function that can be used to
89+ push new content onto the stream. [f] is function that
90+ expects to receive a promise that gets resolved when the user
91+ decides that they have pushed all their content onto the stream.
92+ When the promise forwarded to [f] gets resolved, the stream will be
93+ closed. *)
94+ let f, push = App.create_stream () in
95+ let timers =
96+ List.map
97+ (fun t ->
98+ Lwt_unix.sleep t
99+ >|= fun () -> push (Printf.sprintf "Hello after %f seconds\n" t))
100+ [1.; 2.; 3.]
101+ in
102+ f (Lwt.join timers))
103+
85104let default =
86- not_found (fun req ->
105+ not_found (fun _req ->
87106 `Json Ezjsonm.(dict [("message", string "Route not found")]) |> respond')
88107
89108let print_person =
@@ -93,7 +112,9 @@ let print_person =
93112 in
94113 `Json (person |> json_of_person) |> respond')
95114
96- let _ = App.empty |> print_param |> print_person |> default |> App.run_command
115+ let _ =
116+ App.empty |> print_param |> print_person |> streaming |> default
117+ |> App.run_command
97118```
98119
99120compile and run with:
Original file line number Diff line number Diff line change @@ -13,6 +13,12 @@ let print_param =
1313let streaming =
1414 let open Lwt.Infix in
1515 get " /hello/stream" (fun _req ->
16+ (* [create_stream] returns a push function that can be used to
17+ push new content onto the stream. [f] is function that
18+ expects to receive a promise that gets resolved when the user
19+ decides that they have pushed all their content onto the stream.
20+ When the promise forwarded to [f] gets resolved, the stream will be
21+ closed. *)
1622 let f, push = App. create_stream () in
1723 let timers =
1824 List. map
You can’t perform that action at this time.
0 commit comments