Skip to content

Instantly share code, notes, and snippets.

@jcrossley3
Created January 15, 2015 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcrossley3/98b0569361d19e3f0433 to your computer and use it in GitHub Desktop.
Save jcrossley3/98b0569361d19e3f0433 to your computer and use it in GitHub Desktop.
(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