Skip to content

Commit

Permalink
[mod] [taoensso#424] reintroduce fixed jetty9-ring-adapter support
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoKramer committed Apr 11, 2023
1 parent 561097c commit a611614
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
13 changes: 6 additions & 7 deletions example-project/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@
[com.taoensso/timbre "6.1.0"]

;;; TODO Choose (uncomment) a supported web server -----------------------
[http-kit "2.6.0"] ; Default
;; [org.immutant/web "x.y.z"
;; [http-kit "2.6.0"] ; Default
;; [org.immutant/web "2.1.10"
;; :exclusions [ring/ring-core]]
;; [nginx-clojure/nginx-clojure-embed "x.y.z"] ; Needs v0.4.2+
;; [aleph "x.y.z"]
;; [info.sunng/ring-jetty9-adapter "x.y.z"]
[info.sunng/ring-jetty9-adapter "0.18.5"]
[commons-io/commons-io "2.11.0"]
;; [nginx-clojure/nginx-clojure-embed "0.5.2"] ; Needs v0.4.2+
;; [aleph "0.4.1"]
[info.sunng/ring-jetty9-adapter "0.18.5"]
[commons-io/commons-io "2.11.0"]
;; -----------------------------------------------------------------------

[ring "1.10.0"]
Expand Down
29 changes: 14 additions & 15 deletions example-project/src/example/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@
;;;; Define our Sente channel socket (chsk) server

(let [;; Serialization format, must use same val for client + server:
packer :edn ; Default packer, a good choice in most cases
packer :edn] ; Default packer, a good choice in most cases
;; (sente-transit/get-transit-packer) ; Needs Transit dep
]


(defonce chsk-server
(sente/make-channel-socket-server!
(get-sch-adapter) {:packer packer})))
(get-sch-adapter) {:packer packer
:csrf-token-fn nil})))

(let [{:keys [ch-recv send-fn connected-uids
ajax-post-fn ajax-get-or-ws-handshake-fn]}
Expand All @@ -68,8 +69,8 @@
(defonce ring-ajax-get-or-ws-handshake ajax-get-or-ws-handshake-fn)
(defonce ch-chsk ch-recv) ; ChannelSocket's receive channel
(defonce chsk-send! send-fn) ; ChannelSocket's send API fn
(defonce connected-uids connected-uids) ; Watchable, read-only atom
)
(defonce connected-uids connected-uids)) ; Watchable, read-only atom


;; We can watch this atom for changes if we like
(add-watch connected-uids :connected-uids
Expand Down Expand Up @@ -115,8 +116,8 @@
[:p
[:input#input-login {:type :text :placeholder "User-id"}] " "
[:button#btn-login {:type "button"} "← Log in with user-id"]]
[:script {:src "main.js"}] ; Include our cljs target
))
[:script {:src "main.js"}])) ; Include our cljs target


(defn login-handler
"Here's where you'll add your server-side login/auth procedure (Friend, etc.).
Expand Down Expand Up @@ -186,15 +187,15 @@

(defmulti -event-msg-handler
"Multimethod to handle Sente `event-msg`s"
:id ; Dispatch on event-id
)
:id) ; Dispatch on event-id


(defn event-msg-handler
"Wraps `-event-msg-handler` with logging, error catching, etc."
[{:as ev-msg :keys [id ?data event]}]
(-event-msg-handler ev-msg) ; Handle event-msgs on a single thread
(-event-msg-handler ev-msg)) ; Handle event-msgs on a single thread
;; (future (-event-msg-handler ev-msg)) ; Handle event-msgs on a thread pool
)


(defmethod -event-msg-handler
:default ; Default/fallback case (no other matching handler)
Expand Down Expand Up @@ -266,11 +267,9 @@
;; [(aleph.netty/port server)
;; (fn [] (.close ^java.io.Closeable server) (deliver p nil))])

(let [#_#_ws-handshake (:ajax-get-or-ws-handshake-fn (sente/make-channel-socket! (get-sch-adapter)))
server (jetty/run-jetty ring-handler {:port port
(let [server (jetty/run-jetty ring-handler {:port port
:async? true
:join? false
#_#_:websockets {"/chsk" ws-handshake}})]
:join? false})]
[port (fn [] (jetty/stop-server server))])
;; ------------------------------------------------------------------

Expand Down
13 changes: 6 additions & 7 deletions src/taoensso/sente/server_adapters/jetty9.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@
(jetty9.websocket/send! sch msg (ajax-cbs sch)))))

(defn- server-ch-resp
[websocket? {:keys [on-open on-close on-msg on-error]}]
[websocket? {:keys [on-open on-close on-msg on-error ring-async-resp-fn ring-async-raise-fn]}]
{: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))))
:on-close (fn [sch status _] (on-close sch websocket? status))
:ring-async-resp-fn ring-async-resp-fn
:ring-async-raise-fn ring-async-raise-fn})

(deftype JettyServerChanAdapter []
i/IServerChanAdapter
(ring-req->server-ch-resp [_ req callbacks-map]
(server-ch-resp (websocket-req? req) callbacks-map)))
(jetty9.websocket/ws-upgrade-response
(server-ch-resp (jetty9.websocket/ws-upgrade-request? req) callbacks-map))))

(defn get-sch-adapter [] (JettyServerChanAdapter.))

0 comments on commit a611614

Please sign in to comment.