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-09 21:26:35 +00:00
|
|
|
(defn add-client! [ws-channel to from]
|
|
|
|
(println "Client connected:" (.toString ws-channel))
|
|
|
|
(httpkit/on-close ws-channel
|
|
|
|
(fn [status]
|
|
|
|
(println "Client disconnected:" (.toString ws-channel)
|
|
|
|
"with status" status)
|
|
|
|
(a/close! to)
|
|
|
|
(a/close! from)))
|
|
|
|
(httpkit/on-receive ws-channel #(a/put! from (read-string %)))
|
|
|
|
(a/go-loop []
|
|
|
|
(if-let [event (<! to)]
|
|
|
|
(do
|
|
|
|
(httpkit/send! ws-channel (str event))
|
|
|
|
(recur))
|
|
|
|
(httpkit/close ws-channel))))
|