3

Ocsigen /Eliom チュートリアルは、「Hello, world!」を提供するアプリケーションの例から始まります。HTMLとして:

open Eliom_content.Html5.D

let main_service =
  Eliom_registration.Html5.register_service
    ~path:["graff"]
    ~get_params:Eliom_parameter.unit
    (fun () () ->
      Lwt.return
         (html
           (head (title (pcdata "Page title")) [])
           (body [h1 [pcdata "Graffiti"]])))

代わりに、これを JSON としてどのように提供しますか? 具体的には、JSON サービスを登録する方法と、JSON を生成/シリアル化するために使用するライブラリ/コンビネータ (js_of_ocaml?)

4

2 に答える 2

5

あなたが何をしたいのかよくわかりませんが、JSONについては、次のようにOCaml型を使用して「派生」( Deriving_Jsonを参照)を使用してJSON型を作成できます。

    type deriving_t = (string * string) deriving (Json)

これにより、OCaml 型に対応する JSON 型が作成されます。

この型を使用してサーバーと通信する方法は次のとおりです (サーバー機能がわからない場合は、それに関するドキュメントとサーバー側のクライアント値に関するドキュメントを参照してください)。

    (* first you have to create a server function (this allowed the client to call a function from the server *)
    let json_call =
      server_function
        Json.t<deriving_t>
        (fun (a,b) ->
           Lwt.return (print_endline ("log on the server: "^a^b)))

    (* let say that distillery has already generate all the needed stuff (main_service, Foobar_app, etc..) *)
    let () =
      Foobar_app.register
        ~service:main_service
        (fun () () ->
           {unit{
             (* here I call my server function by using ocaml types directly, it will be automatically serialize *)
             ignore (%json_call ("hello", "world"))
           }};
           Lwt.return
             (Eliom_tools.F.html
                ~title:"foobar"
                ~css:[["css";"foobar.css"]]
                Html5.F.(body [
                   h2 [pcdata "Welcome from Eliom's distillery!"];
               ])))

クライアント/サーバー通信を使用したい場合は、Eliom_bus、Eliom_comet、または Eliom_react を調べてください。

(申し訳ありませんが、2 つ以上のリンクを作成することはできません :) しかし、ドキュメントは ocsigen.org Web サイトにあります)。

それがあなたを助けることを願っています。

于 2013-06-13T15:57:47.187 に答える