ring-jetty-ws provides ring-style handling of WebSocket connections for projects using ring-jetty-adapter.
Like ring, ring-jetty-ws accepts a handler function that is expected to handle all incoming requests. The handler function is passed a request map with the same structure used in ring, and is expected to return response maps that are compatible with those used in ring.
This means that your WebSocket handler function will be mostly compatible with ring middleware and other ring utilities.
WebSocket requests don't include a body, so the request map will not include any body related keys:
Similarly, on the response, only :status, :headers, and :ws-handlers (see below) have meaning. Other keys are ignored.
Any request for a WebSocket upgrade will be passed to the WebSocket handler, and will skip your ring handler. For all other requests, the opposite is true.
Add the following to your project's :dependencies
[genesys-cloud/ring-jetty-ws 1.0.0]
(require '[ring-jetty-ws.ws :as ws])
(defn ws-handler [request]
(if (isGoodWebsocketRequest? request)
{:status 101
;; all handlers are optional.
:ws-handlers {:on-connect (fn [ws]
(log/debug "WebSocket connected" (ws/connected? ws)))
:on-close (fn [ws code reason]
(log/debug "Websocket closed" code reason))
:on-error (fn [ws err]
(log/error err "Websocket connection error!" ws))
:on-text (fn [ws text]
(log/debug "Websocket text recieved, let's echo!")
(ws/send! ws text))
:on-binary (fn [ws bytes offset len]
(log/debug "Websocket binary data recived" len)}}
{:status 400}))
(run-jetty app {:port 8080
:configurator (ws/configurator ws-handler)}))