2013-08-05 15:19:52 +00:00
|
|
|
(ns grub.websocket
|
2013-09-30 10:11:59 +00:00
|
|
|
(:require [grub.db :as db]
|
2013-08-05 15:19:52 +00:00
|
|
|
[org.httpkit.server :as httpkit]
|
2013-09-30 10:11:59 +00:00
|
|
|
[clojure.core.async :as a :refer [<! >! chan go]]))
|
2013-08-05 15:19:52 +00:00
|
|
|
|
2014-08-19 20:24:19 +00:00
|
|
|
(defn disconnected [status ws-channel to from]
|
|
|
|
(println "Client disconnected:" (.toString ws-channel) "with status" status)
|
|
|
|
(a/close! to)
|
|
|
|
(a/close! from))
|
|
|
|
|
|
|
|
(defn add-connected-client! [ws-channel to from]
|
2014-08-09 21:26:35 +00:00
|
|
|
(println "Client connected:" (.toString ws-channel))
|
2014-08-19 20:24:19 +00:00
|
|
|
(a/go-loop [] (if-let [event (<! to)]
|
|
|
|
(do (httpkit/send! ws-channel (pr-str event))
|
|
|
|
(recur))
|
|
|
|
(httpkit/close ws-channel)))
|
2014-08-09 21:26:35 +00:00
|
|
|
(httpkit/on-receive ws-channel #(a/put! from (read-string %)))
|
2014-08-19 20:24:19 +00:00
|
|
|
(httpkit/on-close ws-channel #(disconnected % ws-channel to from)))
|