diff --git a/clj-browserchannel-immutant-adapter/project.clj b/clj-browserchannel-immutant-adapter/project.clj new file mode 100644 index 0000000..96f2f3c --- /dev/null +++ b/clj-browserchannel-immutant-adapter/project.clj @@ -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"]]}}) diff --git a/clj-browserchannel-immutant-adapter/src/clj_browserchannel_immutant_adapter/core.clj b/clj-browserchannel-immutant-adapter/src/clj_browserchannel_immutant_adapter/core.clj new file mode 100644 index 0000000..8d7e5dd --- /dev/null +++ b/clj-browserchannel-immutant-adapter/src/clj_browserchannel_immutant_adapter/core.clj @@ -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)))