eliom
11.1.1
Eliom是在OCAML中構建客戶端/服務器網和移動應用程序的框架。
它既可以用作傳統的服務器端網絡框架,也可以用作複雜的客戶端服務器應用程序。
Eliom將OCAML轉換為多層語言,使其成為一個單個程序的服務器和客戶端的服務器和客戶端部分。
這簡化了服務器和客戶端之間的很多通信。應用程序可以在任何Web瀏覽器或移動設備(iOS,Android)上運行,從而從為每個平台開發一個版本的需求中節省。
Eliom支持反應性頁面(在服務器或客戶端生成),高級會話機制,服務器到客戶端通信,基於持續的Web編程等。
Eliom是Ocsigen項目的一部分。
opam install eliom
在路徑/foo上定義服務,採用任何獲取參數:
let myservice =
Eliom_service. create
~path: ( Eliom_service. Path [ " foo " ])
~meth: ( Eliom_service. Get ( Eliom_parameter. any))
()
let () =
Eliom_registration.Html. register ~service: myservice
( fun get_params () ->
Lwt. return
Eliom_content.Html.F. (html (head (title (txt " " )))
(body [h1 [txt " Hello " ]])))用參數插入指向該服務的鏈接:
Eliom_content.Html.D. a ~service: myservice [txt " Home " ] [( " param1 " , " v1 " ); ( " param2 " , " v2 " )]活動處理程序用OCAML編寫:
div ~a: [a_onclick [ % client ( fun ev -> ... )]] [ ... ]客戶端和服務器側是一個單個程序:
let % server a = ... (* code for the server part of the application *)
let % client b = ... (* code for the client part of the application *)
let % shared c = ... (* code that will be included in both parts *)在客戶端代碼中使用服務器端值:
let % server a = ...
let % client f () =
print_endline ~% a ; (* print in browser console *)
...從客戶端程序調用服務器函數:
let % rpc f (x : int ) : string Lwt. t = ... (* server-side code *)
let % client () =
let % lwt r = f 4 in
...使用ELIOM參考文獻在服務器上保存會話數據:
let % server r = Eliom_reference. eref ~scope: Eliom_common. default_session_scope 0
let % server f () =
let % lwt v = Eliom_reference. get r in
Eliom_reference. set r (v + 1 );
...
範圍可以在哪裡:
Eliom_common.default_session_scope (每個瀏覽器的不同值),Eliom_common.default_process_scope (每個選項卡的不同值),Eliom_common.default_group_scope (每個用戶的不同值),Eliom_common.site_scope (整個站點的值),Eliom_common.global_scope (整個服務器的全局值)。如果您將可選參數~persistent添加到函數Eliom_reference.eref ,則ELIOM參考是持久的。 這裡更多文檔。
使用Ocsigen開始,用Eliom編寫第一個網絡和移動應用程序