Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor updates to web + highlight http-kit diffs
- Loading branch information
1 parent
08b03a0
commit 2e389b9
Showing
2 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters