Created
January 15, 2015 18:02
-
-
Save jcrossley3/98b0569361d19e3f0433 to your computer and use it in GitHub Desktop.
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
(def chunked-stream | |
(async/as-channel | |
{:on-open | |
(fn [stream] | |
(.start | |
(Thread. | |
(fn [] | |
(async/send! stream "[" false) | |
(dotimes [n 10] | |
;; we have to send a few bytes with each | |
;; response - there is a min-bytes threshold to | |
;; trigger data to the client | |
(async/send! stream (format "%s ;; %s\n" n (repeat 128 "1")) false)) | |
;; 2-arity send! closes the stream | |
(async/send! stream "]")))))})) | |
(def non-chunked-stream | |
(async/as-channel | |
{:on-open | |
(fn [stream] | |
(async/send! stream (str (repeat 128 "1"))))})) | |
(def ws-as-channel | |
(async/as-channel | |
{:on-open (fn [ch hs] | |
#_(println "TC: open" ch hs)) | |
:on-message (fn [ch message] | |
#_(println "TC: message" message) | |
(async/send! ch (.toUpperCase message))) | |
:on-error (fn [ch err] | |
(println "Error on websocket") | |
(.printStackTrace err)) | |
:on-close (fn [ch reason] | |
#_(println "TC: closed" reason))})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment