Ring adapter for jetty9, which supports websocket and http2
Clojure
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
dev-resources
doc
examples/rj9a
src/ring/adapter
test/ring/adapter
.gitignore
README.md
project.clj

README.md

ring-jetty9-adapter (rj9a)

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.

Usage

Leiningen

latest version on clojars

Code

In the REPL:

(require '[ring.adapter.jetty9 :refer [run-jetty]])
(run-jetty app {:port 50505}) ;; same as the 'official' adapter of jetty 7

In ns declaration:

(ns my.server
  (:require [ring.adapter.jetty9 :refer [run-jetty]]))

WebSocket

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])})

WebSocketProtocol allows you to read and write data on the ws value:

  • (send! ws msg)
  • (close! ws)
  • (remote-addr ws)
  • (idle-timeout! ws timeout)

Notice that we support different type of msg:

  • byte[] and ByteBuffer: send binary websocket message
  • String and other Object: send text websocket message
  • (fn [ws]) (clojure function): Custom function you can operate on Jetty's RemoteEndpoint

There is a new option :websockets available. Accepting a map of context path and listener class:

(use 'ring.adapter.jetty9)
(run-jetty app {:websockets {"/loc" ws-handler}})

In the 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 = ....

Examples

You can find examples in examples folder. To run example:

  • http2: lein with-profile default,example-http2 run (NOTE that your will need JDK8 to run this example)
  • websocket: lein with-profile default,example-websocket run

Contributors

License

Copyright © 2013-2016 Sun Ning

Distributed under the Eclipse Public License, the same as Clojure.