Skip to content

Instantly share code, notes, and snippets.

@tobias

tobias/core.clj Secret

Created Apr 15, 2016
Embed
What would you like to do?
(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