forked from taoensso/sente
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "[mod] [taoensso#424] [BREAKING] Temporarily remove `jetty9-ri…
…ng-adapter` support" This reverts commit c3d7c6c.
- Loading branch information
1 parent
bddcf65
commit acdcf35
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
(ns taoensso.sente.server-adapters.jetty9 | ||
"Sente adapter for ring-jetty9-adapter, | ||
(https://github.com/sunng87/ring-jetty9-adapter). | ||
Note that ring-jetty9-adapter defines WebSocket routes/handlers | ||
separately from regular Ring routes/handlers [1,2]. | ||
This can make it tricky to set up stateful middleware correctly | ||
(for example as you may want to do for CSRF protection). | ||
See [3] for a full example. | ||
[1] https://github.com/sunng87/ring-jetty9-adapter/blob/master/examples/rj9a/websocket.clj | ||
[2] https://github.com/sunng87/ring-jetty9-adapter/issues/41#issuecomment-630206233 | ||
[3] https://gist.github.com/wavejumper/40c4cbb21d67e4415e20685710b68ea0" | ||
|
||
{:author "Thomas Crowley (@wavejumper)"} | ||
(:require [clojure.string :as str] | ||
[ring.adapter.jetty9.websocket :as jetty9.websocket] | ||
[taoensso.sente.interfaces :as i])) | ||
|
||
(defn- ajax-cbs [sch] | ||
{:write-failed (fn [_throwable] (jetty9.websocket/close! sch)) | ||
:write-success (fn [ ] (jetty9.websocket/close! sch))}) | ||
|
||
(extend-type org.eclipse.jetty.websocket.api.WebSocketAdapter | ||
i/IServerChan | ||
(sch-open? [sch] (jetty9.websocket/connected? sch)) | ||
(sch-close! [sch] (jetty9.websocket/close! sch)) | ||
(sch-send! [sch websocket? msg] | ||
(if websocket? | ||
(jetty9.websocket/send! sch msg) | ||
(jetty9.websocket/send! sch msg (ajax-cbs sch))))) | ||
|
||
(defn- server-ch-resp | ||
[websocket? {:keys [on-open on-close on-msg on-error]}] | ||
{:on-connect (fn [sch ] (on-open sch websocket?)) | ||
:on-text (fn [sch msg ] (on-msg sch websocket? msg)) | ||
:on-error (fn [sch error ] (on-error sch websocket? error)) | ||
:on-close (fn [sch status _] (on-close sch websocket? status))}) | ||
|
||
(defn- websocket-req? [ring-req] | ||
(when-let [s (get-in ring-req [:headers "upgrade"])] | ||
(= "websocket" (str/lower-case s)))) | ||
|
||
(deftype JettyServerChanAdapter [] | ||
i/IServerChanAdapter | ||
(ring-req->server-ch-resp [_ req callbacks-map] | ||
(server-ch-resp (websocket-req? req) callbacks-map))) | ||
|
||
(defn get-sch-adapter [] (JettyServerChanAdapter.)) |