add clj-browserchannel-immutant-adapter
This commit is contained in:
parent
02b2f551e1
commit
1ade049b2b
7
clj-browserchannel-immutant-adapter/project.clj
Normal file
7
clj-browserchannel-immutant-adapter/project.clj
Normal file
|
@ -0,0 +1,7 @@
|
|||
(defproject clj-browserchannel-immutant-adapter "0.0.1"
|
||||
:description "Immutant async adapter for BrowserChannel"
|
||||
:dependencies [[org.immutant/web "2.1.4"]
|
||||
[net.thegeez/clj-browserchannel-server "0.2.2"]]
|
||||
:profiles {:provided
|
||||
{:dependencies
|
||||
[[org.clojure/clojure "1.8.0"]]}})
|
|
@ -0,0 +1,37 @@
|
|||
(ns clj-browserchannel-immutant-adapter.core
|
||||
(:require
|
||||
[immutant.web :as iweb]
|
||||
[immutant.web.async :as iasync]
|
||||
[net.thegeez.async-adapter :as bc-async-adapter]))
|
||||
|
||||
(deftype ImmutantResponse [channel]
|
||||
bc-async-adapter/IAsyncAdapter
|
||||
(head [this status headers]
|
||||
(let [headers (assoc headers "Transfer-Encoding" "chunked")]
|
||||
(iasync/send! channel {:status status :headers headers})))
|
||||
(write-chunk [this data]
|
||||
(if (iasync/open? channel)
|
||||
(iasync/send! channel data)
|
||||
(throw bc-async-adapter/ConnectionClosedException)))
|
||||
(close [this]
|
||||
(iasync/close channel)))
|
||||
|
||||
(defn wrap-immutant-async-adapter
|
||||
[handler]
|
||||
(fn [request]
|
||||
(let [resp (handler request)]
|
||||
(if (= :http (:async resp))
|
||||
(iasync/as-channel
|
||||
request
|
||||
{:on-open
|
||||
(fn [channel]
|
||||
(let [reactor (:reactor resp)
|
||||
emit (ImmutantResponse. channel)]
|
||||
(reactor emit)))})
|
||||
|
||||
resp))))
|
||||
|
||||
(defn run-immutant [handler options]
|
||||
(-> handler
|
||||
(wrap-immutant-async-adapter)
|
||||
(iweb/run options)))
|
Reference in a new issue