Skip to content

Instantly share code, notes, and snippets.

@ztellman
Created May 19, 2015 02:56
Show Gist options
  • Save ztellman/dd4bc086413e02c98466 to your computer and use it in GitHub Desktop.
Save ztellman/dd4bc086413e02c98466 to your computer and use it in GitHub Desktop.
(ns aleph-test.core
(:require [aleph.http :as aleph]
[manifold.stream :as stream]
[manifold.deferred :as deferred]
[compojure.core :as compojure]
[compojure.route :as compojure-route]
[ring.middleware.params :as ring-params]))
(def not-websocket
{:status 400
:headers {:content-type "application/text"}
:body "You weren't a websocket request....'"})
(defn echo-handler
[req]
(if-let [socket (try @(aleph/websocket-connection req)
(catch Exception e nil))]
(stream/connect socket socket)
not-websocket))
(defonce callbacks (atom []))
(defn fun-handler
[req]
(deferred/let-flow [ms (aleph/websocket-connection req)]
(stream/on-closed ms #(swap! callbacks conj "Goodbye"))
(stream/put! ms "Hello")
(stream/connect ms ms)))
(def handler
(ring-params/wrap-params
(compojure/routes
(compojure/GET "/echo" [] echo-handler)
(compojure/GET "/fun" [] fun-handler)
(compojure-route/not-found "Bugger."))))
(def s (aleph/start-server handler {:port 9876}))
(def c (aleph/websocket-client "ws://localhost:9876/fun"))
;; (stream/put! @c "Working fine..")
;; (stream/take! @c)
;; (stream/take! @c)
;; (stream/close! @c)
;; (stream/closed? @c)
;; Why doesn't it say "Goodbye."? :(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment