-
Notifications
You must be signed in to change notification settings - Fork 10
/
http_kit.clj
48 lines (43 loc) · 1.57 KB
/
http_kit.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(ns ripley.live.impl.http-kit
"http-kit WebSocket connection implementation."
(:require [ripley.live.protocols :as p]
[org.httpkit.server :as server]
[clojure.tools.logging :as log]
[ring.middleware.params :as params]))
(defn- send-fn! [chd data]
(let [ch @chd]
(server/send!
ch
(if (server/websocket? ch)
data
(str "data: " data "\n\n"))
false)))
(defn handler [uri options initialize-connection]
(params/wrap-params
(fn [req]
(when (= uri (:uri req))
(try
(let [id (-> req :query-params (get "id") java.util.UUID/fromString)
ch* (object-array 1)
send! (partial send-fn! (delay (aget ch* 0)))
callbacks (initialize-connection id send!)]
(server/as-channel
req
{:on-open
(fn [ch]
(aset ch* 0 ch)
(log/debug "Connected, ws? " (server/websocket? ch))
;; If connection is not WebSocket, initialize SSE headers
(when-not (server/websocket? ch)
(server/send! ch {:status 200
:headers {"Content-Type" "text/event-stream"}}
false))
(p/on-open callbacks))
:on-close
(fn [_ch status]
(p/on-close callbacks status))
:on-receive
(fn [_ch ^String data]
(p/on-receive callbacks data))}))
(catch Exception e
(log/error e "Unable to initialize live context")))))))