ws.cljs 683 B

123456789101112131415161718192021
  1. (ns bombnet.web.ws
  2. (:require [cljs.core.async :refer [<! >! chan]])
  3. (:require-macros [cljs.core.async.macros :refer [go]]))
  4. (defn connect [room id]
  5. (let [in-ch (chan)
  6. out-ch (chan)
  7. host (.. js/window -location -host)
  8. socket (js/WebSocket. (str "ws://" host "/ws/" room "?" id))]
  9. (go (loop []
  10. (.send socket (.stringify js/JSON (clj->js (<! out-ch))))
  11. (recur)))
  12. (aset socket "onmessage"
  13. (fn [message]
  14. (go
  15. (>! in-ch (js->clj (.parse js/JSON (.. message -data))
  16. :keywordize-keys true)))))
  17. [in-ch out-ch]))
  18. (defn send! [[in out] msg]
  19. (go (>! out msg)))