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 Mar 21, 2023
1 parent acdcf35 commit 0317450
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
5 changes: 3 additions & 2 deletions example-project/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
[com.taoensso/timbre "6.1.0"]

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

[ring "1.9.6"]
Expand Down
32 changes: 21 additions & 11 deletions example-project/src/example/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
[taoensso.sente :as sente]

;;; TODO Choose (uncomment) a supported web server + adapter -------------
[org.httpkit.server :as http-kit]
[taoensso.sente.server-adapters.http-kit :refer [get-sch-adapter]]
;;[org.httpkit.server :as http-kit]
;;[taoensso.sente.server-adapters.http-kit :refer [get-sch-adapter]]

;; [immutant.web :as immutant]
;; [taoensso.sente.server-adapters.immutant :refer [get-sch-adapter]]
Expand All @@ -27,8 +27,9 @@
;; [aleph.http :as aleph]
;; [taoensso.sente.server-adapters.aleph :refer [get-sch-adapter]]

;; [ring.adapter.jetty9.websocket :as jetty9.websocket]
;; [taoensso.sente.server-adapters.jetty9 :refer [get-sch-adapter]]
[ring.adapter.jetty9 :as jetty]
[ring.adapter.jetty9.websocket :as jetty9.websocket]
[taoensso.sente.server-adapters.jetty9 :refer [get-sch-adapter]]
;;
;; See https://gist.github.com/wavejumper/40c4cbb21d67e4415e20685710b68ea0
;; for full example using Jetty 9
Expand All @@ -52,13 +53,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 Down Expand Up @@ -245,13 +247,13 @@
(defn stop-web-server! [] (when-let [stop-fn @web-server_] (stop-fn)))
(defn start-web-server! [& [port]]
(stop-web-server!)
(let [port (or port 0) ; 0 => Choose any available port
(let [port (or port 3333) ; 0 => Choose any available port
ring-handler (var main-ring-handler)

[port stop-fn]
;;; TODO Choose (uncomment) a supported web server ------------------
(let [stop-fn (http-kit/run-server ring-handler {:port port})]
[(:local-port (meta stop-fn)) (fn [] (stop-fn :timeout 100))])
;;(let [stop-fn (http-kit/run-server ring-handler {:port port})]
;; [(:local-port (meta stop-fn)) (fn [] (stop-fn :timeout 100))]]
;;
;; (let [server (immutant/run ring-handler :port port)]
;; [(:port server) (fn [] (immutant/stop server))])
Expand All @@ -265,6 +267,13 @@
;; ;; (aleph.netty/wait-for-close server)
;; [(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
:async? true
:join? false
:websockets {"/chsk" ws-handshake}})]
[port (fn [] (jetty/stop-server server))])
;; ------------------------------------------------------------------

uri (format "http://localhost:%s/" port)]
Expand All @@ -283,4 +292,5 @@

(comment
(start!) ; Eval this at REPL to start server via REPL
(test-fast-server>user-pushes))
(test-fast-server>user-pushes)
(stop!))
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 0317450

Please sign in to comment.