Ring adapter for jetty9, which supports websocket and http2
Ring adapter for Jetty 9 with HTTP2 and WebSocket support.
This is a simple and plain wrapper on Jetty 9. It doesn't introduce additional thread model or anything else (no unofficial ring variance, no core.async). You are free to add those on top of our base API.
As of Ring 1.6, the official Jetty adapter has been updated to Jetty 9.2. However, rj9a tracks most recent Jetty release and offers additional features like http/2 and websocket.
In the REPL:
(require '[ring.adapter.jetty9 :refer [run-jetty]]) (run-jetty app {:port 50505}) ;; same as the 'official' adapter
In ns declaration:
(ns my.server (:require [ring.adapter.jetty9 :refer [run-jetty]]))
(require '[ring.adapter.jetty9 :refer [run-jetty]])(defn app [request send-response raise-error] (send-response {:body "It works!"})) (run-jetty app {:port 50505 :async? true})
If you use plain socket http 1.1 only, for example, behind an nginx with ssl off-loading, you can exclude HTTPs dependencies to reduce the uberjar size:
:exclusions [org.eclipse.jetty/jetty-alpn-conscrypt-server org.conscrypt/conscrypt-openjdk-uber]
ALPN is required for HTTP/2 transport, you will need additional dependency to enable ALPN.
[org.eclipse.jetty/jetty-alpn-java-server ~jetty-version]
[org.eclipse.jetty/jetty-alpn-openjdk8-server ~jetty-version]
example-http2-legacyprofile in project.clj for boot-classpath configuration
[org.eclipse.jetty/jetty-alpn-conscrypt-server ~jetty-version]but it's not recommended for now because of memory leak issue
Note your will need to replace
~jetty-versionwith corresponding jetty version that your version of rj9a uses.
To enable HTTP/2 on cleartext and secure transport, you can simply add options to
run-jettylike:
(jetty/run-jetty dummy-app {:port 5000 :h2c? true ;; enable cleartext http/2 :h2? true ;; enable http/2 :ssl? true ;; ssl is required for http/2 :ssl-port 5443 :keystore "dev-resources/keystore.jks" :key-password "111111" :keystore-type "jks"})
You can define following handlers for websocket events.
(def ws-handler {:on-connect (fn [ws]) :on-error (fn [ws e]) :on-close (fn [ws status-code reason]) :on-text (fn [ws text-message]) :on-bytes (fn [ws bytes offset len]) :on-ping (fn [ws bytebuffer]) :on-pong (fn [ws bytebuffer])})
WebSocketProtocol allows you to read and write data on the
wsvalue:
Notice that we support different type of msg:
A callback can also be specified for
send!:
(send! ws msg {:write-failed (fn [throwable]) :write-success (fn [])})
A callback is a map where keys
:write-failedand
:write-successare optional.
There is a new option
:websocketsavailable. Accepting a map of context path and listener class:
clojure (use 'ring.adapter.jetty9) (run-jetty app {:websockets {"/loc" ws-handler}})
In the javascript:
javascript // remember to add the trailing slash. // Otherwise, jetty will return a 302 on websocket upgrade request, // which is not supported by most browsers. var ws = new WebSocket("ws://somehost/loc/"); ws.onopen = ....
If you want to omit the trailing slash from your URLs (and not receive a redirect from Jetty), you can start the server like:
clojure (run-jetty app {:websockets {"/loc" ws-handler} :allow-null-path-info true})
Sometimes you may have a negotiation with the websocket client on the handshake (upgrade request) phase. You can define a ring like function that returns the websocket handler, or raises an error. You may also select a subprotocol from
(:subprotocol request)and configure available
(:extensions request). See websocket example for detail.
You can find examples in
examplesfolder. To run example:
lein with-profile default,example-http runa very basic example of ring handler
lein with-profile default,example-async runring 1.6 async handler example
lein with-profile example-http2-openjdk8 run
lein with-profile example-http2 run
lein with-profile default,example-websocket run
Copyright © 2013-2017 Sun Ning
Distributed under the Eclipse Public License, the same as Clojure.