123456789101112131415161718192021 |
- (ns bombnet.web.ws
- (:require [cljs.core.async :refer [<! >! chan]])
- (:require-macros [cljs.core.async.macros :refer [go]]))
- (defn connect [room id]
- (let [in-ch (chan)
- out-ch (chan)
- host (.. js/window -location -host)
- socket (js/WebSocket. (str "ws://" host "/ws/" room "?" id))]
- (go (loop []
- (.send socket (.stringify js/JSON (clj->js (<! out-ch))))
- (recur)))
- (aset socket "onmessage"
- (fn [message]
- (go
- (>! in-ch (js->clj (.parse js/JSON (.. message -data))
- :keywordize-keys true)))))
- [in-ch out-ch]))
- (defn send! [[in out] msg]
- (go (>! out msg)))
|