Skip to content

Commit

Permalink
Minor updates to web + highlight http-kit diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrossley3 committed Jul 20, 2015
1 parent 08b03a0 commit 2e389b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
38 changes: 38 additions & 0 deletions src/demo/http_kit.clj
@@ -0,0 +1,38 @@
(ns demo.http-kit
"Show API differences between Immutant and HTTP Kit"
(:require [immutant.web :as web]
[immutant.web.async :as async]))

#_(defn async-handler
"For comparison, the actual http-kit example"
[ring-request]
;; unified API for WebSocket and HTTP long polling/streaming
(with-channel ring-request channel ; get the channel
(if (websocket? channel) ; if you want to distinguish them
(on-receive channel (fn [data] ; two way communication
(send! channel data)))
(send! channel {:status 200
:headers {"Content-Type" "text/plain"}
:body "Long polling?"}))))

(defn async-handler
"Immutant version of http-kit example showing websocket degrade
Differences:
- Channel passed as callback param instead of macro param
- Channel not closed after send, by default
- All ring body types supported
- Automatic chunking of large (>16K) content"
[ring-request]
(async/as-channel ring-request
{:on-message (fn [channel data]
(async/send! channel data))
:on-open (fn [channel]
(if-not (:websocket? ring-request)
(async/send! channel {:status 200
:headers {"Content-Type" "text/plain"}
:body "Long polling?"}
:close? true)))}))

(defn -main [& {:as args}]
(web/run async-handler :path "/http-kit"))
16 changes: 10 additions & 6 deletions src/demo/web.clj
Expand Up @@ -16,7 +16,8 @@
(content-type "text/plain")))

(defn counter
"From https://github.com/ring-clojure/ring/wiki/Sessions"
"An example manipulating session state from
https://github.com/ring-clojure/ring/wiki/Sessions"
[{session :session}]
(let [count (:count session 0)
session (assoc session :count (inc count))]
Expand All @@ -25,6 +26,7 @@
(assoc :session session))))

(defn sse-countdown
"An example of Server Sent Events"
[request]
(sse/as-channel request
{:on-open (fn [ch]
Expand All @@ -34,26 +36,28 @@
;; Signal the client to call EventSource.close()
(sse/send! ch {:event "close", :data "bye!"}))}))


;;; Compose routes from above functions
(defroutes routes
(GET "/" {c :context} (redirect (str c "/index.html")))
(GET "/counter" [] counter)
(GET "/sse" [] sse-countdown)
(route/resources "/")
(ANY "*" [] echo))

(def websocket-callbacks
"WebSocket callback functions"
(def demo-app-callbacks
"Callbacks for websocket.html/app.js example"
{:on-open (fn [channel]
(async/send! channel "Ready to reverse your messages!"))
:on-close (fn [channel {:keys [code reason]}]
(println "close code:" code "reason:" reason))
:on-message (fn [ch m]
(async/send! ch (apply str (reverse m))))})
:on-message (fn [channel m]
(async/send! channel (apply str (reverse m))))})

(defn -main [& {:as args}]
(web/run
(-> routes
(immutant/wrap-session {:timeout 20})
(immutant/wrap-websocket websocket-callbacks))
(immutant/wrap-websocket demo-app-callbacks))
(merge {"host" (env :demo-web-host), "port" (env :demo-web-port)}
args)))

0 comments on commit 2e389b9

Please sign in to comment.