Skip to content

Instantly share code, notes, and snippets.

@tobias

tobias/core.clj Secret

Created April 15, 2016 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tobias/a82e004d0180cbc4db20c9f2f6bf7249 to your computer and use it in GitHub Desktop.
Save tobias/a82e004d0180cbc4db20c9f2f6bf7249 to your computer and use it in GitHub Desktop.
(ns add-extensions.core
(:require [clojure.java.io :as io]
[immutant.web :as web]
[immutant.web.async :as async]
[immutant.web.undertow :as utow])
(:import org.projectodd.wunderboss.web.undertow.async.websocket.UndertowWebsocket
io.undertow.websockets.extensions.PerMessageDeflateHandshake))
(defn extension []
(proxy [PerMessageDeflateHandshake] []
(accept [ex]
;; just to know we've actually registered something w/o having
;; to use deflate
(println "PerMessageDeflateHandshake used")
(proxy-super accept ex))))
(defn echo
[req]
(if (:websocket? req)
(async/as-channel req
:on-open (partial println "OPEN")
:on-message async/send!)
{:status 200
:body (slurp (io/resource "index.html"))}))
(defn -main [& args]
;; create an HttpHandler around the ring handler
(let [handler (utow/http-handler echo)]
(.addExtension
;; look up the WebSocketProtocolHandshakeHandler it uses
(.getAttachment handler UndertowWebsocket/HANDSHAKE_ATTACHMENT_KEY)
(extension))
(web/run handler)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment