Last active
August 29, 2015 14:24
-
-
Save ThomasBrittain/80075f329f443f9b6c4e to your computer and use it in GitHub Desktop.
Attempt at client/server communication in Eliom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{shared{ | |
open Eliom_lib | |
open Eliom_content | |
open Html5 | |
open F | |
}} | |
(* int_list functions *) | |
let int_list = ref [0] | |
let add_int i = (int_list := i :: !int_list) | |
let string_of_int_list () = List.fold_left (fun s i -> s ^ ", " ^ (string_of_int i)) "" !int_list | |
(* Reactive programming setup *) | |
let int_event, int_send = React.E.create () | |
(* Server adding to the int_list *) | |
let rec add_new_int i = | |
add_int i; | |
int_send i; | |
Lwt_unix.sleep 1.0 | |
>>= fun () -> add_new_int (i+1) | |
let _ = add_new_int 1 | |
module Mysite_app = | |
Eliom_registration.App ( | |
struct | |
let application_name = "mysite" | |
end) | |
(* Action for client to add int to int_list *) | |
let new_int_from_client = | |
Eliom_registration.Action.register_post_coservice' | |
~options:`NoReload | |
~post_params:(Eliom_parameter.int "client_int") | |
(fun () client_int -> | |
Lwt.return @@ add_int client_int | |
>>= fun () -> Lwt.return @@ int_send client_int) | |
(* Post form for client to add new int to int_list *) | |
let new_int_form = | |
Html5.F.post_form ~service:new_int_from_client ~port:8080 | |
( | |
fun nothing -> | |
[int_button ~name:nothing ~value:9999 [pcdata "Add an integer!"]; | |
] | |
) | |
(* Register main page *) | |
let main = | |
Mysite_app.register_service | |
~path:[""] | |
~get_params:Eliom_parameter.unit | |
(fun () () -> | |
let e = Eliom_react.Down.of_react int_event in | |
let cldiv = D.div [pcdata "Initial cldiv"] in | |
ignore | |
{unit React.event{ | |
React.E.map (fun (i : int) -> | |
Eliom_content.Html5.Manip.replaceChildren %cldiv [(F.div [pcdata (string_of_int i)])]) %e | |
}}; | |
Lwt.return | |
Html5.(D.html | |
(D.head (title (pcdata "title")) []) | |
(D.body | |
[D.h3 [pcdata "Current Orders:"]; | |
D.h4 [pcdata (string_of_int_list ())]; | |
new_int_form (); | |
cldiv; | |
]))) |
Error corrected. Fixed with the use of replaceChildren
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The server properly updates a list of integers, one integer for each second.
The client can successfully add 9999 to the list.
When the page is refreshed, the initial cldiv shows up as "Initial cldiv" on the page and then is updated with the most recent integer from the server. The server keeps counting, but no more updates are made to cldiv.
Press refresh several times to see the issue. The intended results are for cldiv to be updated each time the server appends a new integer to the list.