socket
This commit is contained in:
@@ -1,12 +1,59 @@
|
||||
(ns main)
|
||||
(ns main
|
||||
(:require
|
||||
["react-ace$default" :as AceEditor]
|
||||
["ace-builds/src-noconflict/mode-haskell"]
|
||||
["ace-builds/src-noconflict/theme-solarized_light"]
|
||||
["ace-builds/src-noconflict/keybinding-vim"]
|
||||
[wscljs.client :as ws]
|
||||
[wscljs.format :as fmt]
|
||||
[clojure.string :as str]
|
||||
[cljs.core.match :refer-macros [match]]))
|
||||
|
||||
(defn display-errors [es]
|
||||
(doseq [{{e :contents} :diagnostic} es]
|
||||
(let [fmte (map #(str " • " % "\n") e)]
|
||||
(js/console.warn (apply str "message from rlpc:\n" fmte)))))
|
||||
|
||||
(defn on-message [e]
|
||||
(let [r (js->clj (js/JSON.parse (.-data e)) :keywordize-keys true)]
|
||||
(match r
|
||||
{:tag "Evaluated" :contents c}
|
||||
(prn c)
|
||||
:else
|
||||
(js/console.warn "unrecognised response from rlp"))))
|
||||
|
||||
(def +rlp-socket+ nil)
|
||||
|
||||
(defn send [msg]
|
||||
(ws/send +rlp-socket+ msg fmt/json))
|
||||
|
||||
(defn on-open []
|
||||
(println "socket opened")
|
||||
(send {:command "evaluate"
|
||||
:source (str/join "\n"
|
||||
["fac n = case (==#) n 0 of"
|
||||
" { <1> -> 1"
|
||||
" ; <0> -> *# n (fac (-# n 1))"
|
||||
" };"
|
||||
""
|
||||
"main = fac 3;"])}))
|
||||
|
||||
(defn init-rlp-socket []
|
||||
(set! +rlp-socket+ (ws/create "ws://127.0.0.1:9002"
|
||||
{:on-message on-message
|
||||
:on-open on-open
|
||||
:on-close #(println "socket closed")
|
||||
:on-error #(println "error: " %)})))
|
||||
|
||||
;; this is called before any code is reloaded
|
||||
(defn ^:dev/before-load stop []
|
||||
(ws/close +rlp-socket+)
|
||||
(js/console.log "stop"))
|
||||
|
||||
;; start is called by init and after code reloading finishes
|
||||
(defn ^:dev/after-load start []
|
||||
(js/console.log "start"))
|
||||
(js/console.log "start")
|
||||
(init-rlp-socket))
|
||||
|
||||
;; init is called ONCE when the page loads
|
||||
;; this is called in the index.html and must be exported
|
||||
|
||||
Reference in New Issue
Block a user