diff --git a/deps.edn b/deps.edn index 4e3b367..1a11b5e 100644 --- a/deps.edn +++ b/deps.edn @@ -2,10 +2,11 @@ "resources"] :deps {org.clojure/clojure {:mvn/version "1.11.1"} ring/ring-anti-forgery {:mvn/version "1.3.0"} - ;com.taoensso/encore {:mvn/version #_"3.21.0" "3.112.0"} ; sente brings an older version - com.taoensso/sente {:mvn/version "1.17.0" #_"1.16.2"} ; later versions dont support jetty anymore + nano-id/nano-id {:mvn/version "1.1.0"} + com.taoensso/sente {:mvn/version "1.20.0-RC1"} org.pinkgorilla/modular {:mvn/version "0.2.60"} ; encoding - org.pinkgorilla/webserver {:mvn/version "0.0.12"} ; middleware + org.pinkgorilla/webserver {:mvn/version "0.0.13"} + ;org.pinkgorilla/webserver {:local/root "../webserver" :deps/manifest :deps} } :aliases diff --git a/src/modular/ws/core.clj b/src/modular/ws/core.clj index fcf112a..58d7da7 100644 --- a/src/modular/ws/core.clj +++ b/src/modular/ws/core.clj @@ -1,7 +1,6 @@ (ns modular.ws.core (:require - [taoensso.timbre :as log :refer [debugf info warn error]] - [taoensso.sente :as sente] + [taoensso.timbre :as log :refer [debugf]] [modular.ws.service.adapter :as adapter] [modular.ws.service.handler :as handler] [modular.ws.service.router :as router] @@ -9,19 +8,17 @@ ;; SERVICE START/STOP -(defn start-websocket-server [server-type sente-debug?] +(defn start-websocket-server [server-type] (let [conn (adapter/ws-init! server-type) bidi-routes (handler/create-bidi-routes conn) router (router/start-router! conn) watch (watch/watch-conn-start conn)] - (when sente-debug? - (reset! sente/debug-mode?_ true)) {:conn conn :bidi-routes bidi-routes :router router :watch watch})) -(defn stop-websocket-server [{:keys [conn bidi-routes router watch] :as this}] +(defn stop-websocket-server [{:keys [_conn _bidi-routes router _watch] :as _this}] (router/stop-router! router)) ;; SEND diff --git a/src/modular/ws/core.cljs b/src/modular/ws/core.cljs index 7c09eb9..5fea300 100644 --- a/src/modular/ws/core.cljs +++ b/src/modular/ws/core.cljs @@ -1,6 +1,6 @@ (ns modular.ws.core (:require - [taoensso.timbre :refer-macros [debug info error]] + [taoensso.timbre :refer-macros [info error]] [re-frame.core :as rf] [modular.ws.service :as service] [modular.ws.ws :as ws])) @@ -44,4 +44,11 @@ (info "ws send (cb): " data) (send! data cb timeout)) (error ":ws/send bad format: " v)) - db)) \ No newline at end of file + db)) + +; todo: remove :ws/send +; 59 goldly/src-unused/system/goldly/component/ui.cljs +; 11 goldly/src-unused/system/goldly/notebook_loader/clj_load.cljs +; 15 devtools/devtools/src/goldly/devtools/websocket.cljs +; 108 oauth2/src/token/identity/dialog.cljs + diff --git a/src/modular/ws/events.cljs b/src/modular/ws/events.cljs index 156ea11..8d1fa82 100644 --- a/src/modular/ws/events.cljs +++ b/src/modular/ws/events.cljs @@ -1,66 +1,11 @@ (ns modular.ws.events (:require - [taoensso.timbre :refer-macros [debug info warn error]] - [cemerick.url :as url] - [re-frame.core :as rf] - [modular.ws.core :refer [start-websocket-client!]])) - -(defn application-url - "gets the current url, as a map" - [] - (url/url (-> js/window .-location .-href))) - -(defn is-https? [] - (let [{:keys [protocol]} (application-url)] - (= "https" protocol))) - -(defn is-served-by-shadow? [db] - (let [app-url (application-url) - port (:port app-url) - shadow-dev-port (get-in db [:config :shadow :dev-http :port]) - shadow-port (get-in db [:config :shadow :http :port])] - (or (= port shadow-dev-port) - (= port shadow-port)))) - -(defn changed-port [db] - (when (is-served-by-shadow? db) - (warn "the page is served by shadow which does not support sente websockets - connecting to jetty server..") - (if (is-https?) - (get-in db [:config :webly/web-server :ssl-port]) - (get-in db [:config :webly/web-server :port])))) - -(rf/reg-event-db - :ws/init - (fn [db [_]] - (let [port (changed-port db)] - (warn "ws connect to port (nil=unchanged): " port) - (start-websocket-client! "/api/chsk" port) - db))) - -#_[{:type :auto - :open? false - :ever-opened? false - :csrf-token "6zxUoCmfhv5lleMLfrHMgpChTHYecrY2TSswTz9YTrLLmm/bn7WWT+NCe4mbEFFfEg+gl/Zyobr7tdQX"} - - {:type :ws - :open? true - :ever-opened? true - :csrf-token "6zxUoCmfhv5lleMLfrHMgpChTHYecrY2TSswTz9YTrLLmm/bn7WWT+NCe4mbEFFfEg+gl/Zyobr7tdQX" - :uid "93732cb7-da5c-4792-88cd-c9362dbed11d" - :handshake-data nil - :first-open? true}] - -;[{:type :auto, - ;:open? false, - ;:ever-opened? false, - ;:csrf-token "2rSXN8g5Pd/ctm/JHj9F4eH85rynsgTj3H69O0mpxi2R/oLnwMFRgEoMGGEkUA09g+6VSFDvEYwXtn2/", - ;:last-ws-error {:udt 1618194904283, :ev #object[Event [object Event]]}, :last-close {:udt 1618194904289, :reason :downgrading-ws-to-ajax}, - ;:last-ws-close {:udt 1618194904303, :ev #object[CloseEvent [object CloseEvent]], :clean? false, :code 1006, :reason ""}, - ;:udt-next-reconnect 1618195624274} + [taoensso.timbre :refer-macros [debug info error]] + [re-frame.core :as rf])) (rf/reg-event-db :ws/state - (fn [db [_ new-state-map old-state-map]] + (fn [db [_ new-state-map _old-state-map]] (debug "ws/state " new-state-map) (when (:first-open? new-state-map) (info "ws open (first-time): " new-state-map) diff --git a/src/modular/ws/msg_handler.clj b/src/modular/ws/msg_handler.clj index ea8b6ad..5209e1d 100644 --- a/src/modular/ws/msg_handler.clj +++ b/src/modular/ws/msg_handler.clj @@ -1,15 +1,15 @@ (ns modular.ws.msg-handler (:require - [taoensso.timbre :refer [tracef debug debugf info infof warn warnf error errorf]])) + [taoensso.timbre :refer [debugf infof error errorf]])) -(defn ws-reply [{:keys [event id ?data ring-req ?reply-fn send-fn] :as req} +(defn ws-reply [{:keys [_event _id _?data _ring-req ?reply-fn _send-fn] :as _req} res] (when ?reply-fn (?reply-fn res))) ;; REPLY -(defn send-response [{:as ev-msg :keys [id ?data ring-req ?reply-fn uid send-fn]} +(defn send-response [{:as _msg :keys [_id _?data _ring-req ?reply-fn uid send-fn]} msg-type response] ;(let [session (:session ring-req) ;uid (:uid session) @@ -27,38 +27,47 @@ :else (error "Cannot send ws-response: neither ?reply-fn nor uid was set!")) (error "Can not send ws-response - msg-type and response have to be set, msg-type:" msg-type "response: " response))) - - - (defmulti -event-msg-handler :id) (defmethod -event-msg-handler :chsk/uidport-open - [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] + [{:as _msg :keys [event _id _?data _ring-req _?reply-fn _send-fn]}] (infof ":chsk/uidport-open: %s" event)) (defmethod -event-msg-handler :chsk/uidport-close - [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] + [{:as _msg :keys [event _id _?data _ring-req _?reply-fn _send-fn]}] (infof ":chsk/uidport-close: %s" event)) (defmethod -event-msg-handler :chsk/ws-ping - [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] - (debugf ":chsk/ws-ping: %s" event)) + [{:as _msg :keys [event _id _?data _ring-req ?reply-fn _send-fn]}] + (infof ":chsk/ws-ping: %s" event) + (when ?reply-fn + (?reply-fn {:unmatched-event-as-echoed-from-server event}))) + +(defmethod -event-msg-handler :chsk/ws-pong + [{:as _msg :keys [event _id _?data _ring-req ?reply-fn _send-fn]}] + (infof ":chsk/ws-pong: %s" event) + (when ?reply-fn + (?reply-fn {:unmatched-event-as-echoed-from-server event}))) + +(defmethod -event-msg-handler :chsk/bad-package + [{:as _msg :keys [event _id _?data _ring-req _?reply-fn _send-fn]}] + (infof ":chsk/bad-package: %s" event)) + +(defmethod -event-msg-handler :chsk/bad-event + [{:as _msg :keys [event _id _?data _ring-req _?reply-fn _send-fn]}] + (infof ":chsk/bad-event: %s" event)) (defmethod -event-msg-handler :default - [{:keys [event id ?data ring-req ?reply-fn send-fn] :as req}] + [{:keys [event id _?data ring-req _?reply-fn _send-fn] :as msg}] (let [session (:session ring-req) - uid (:uid session)] + _uid (:uid session)] (errorf "received websocket message of unknown type: %s event: %s" id event) - (send-response req :ws/unknown event))) + (send-response msg :ws/unknown event))) -(defn event-msg-handler [{:keys [client-id id event ?data uid] :as req}] +(defn event-msg-handler [{:keys [_client-id id event ?data _uid] :as msg}] (debugf "ws rcvd: evt: %s id: %s data: %s" event id ?data) - (when req - (let [msg-type (first event)] - ;(if (is-authorized? msg-type uid) - (-event-msg-handler req) - ;(send-reject-response req msg-type)) - ))) + (when msg + (-event-msg-handler msg))) ; {:client-id "591b690d-5633-48c3-884d-348bbcf5c9ca" ; :uid "3c8e0a40-356c-4426-9391-1445140ff509" diff --git a/src/modular/ws/msg_handler.cljs b/src/modular/ws/msg_handler.cljs index 01e927f..95f1851 100644 --- a/src/modular/ws/msg_handler.cljs +++ b/src/modular/ws/msg_handler.cljs @@ -1,42 +1,34 @@ (ns modular.ws.msg-handler (:require - [taoensso.timbre :as timbre :refer-macros [tracef debug debugf infof info warnf error errorf trace]] + [taoensso.timbre :as timbre :refer-macros [debug debugf infof error]] [re-frame.core :as rf] - [taoensso.encore :as encore :refer-macros [have have?]])) + [taoensso.encore :as encore :refer-macros [have]])) (defmulti -event-msg-handler :id) (defmethod -event-msg-handler :chsk/handshake - [{:as ev-msg :keys [?data]}] - (let [[?uid ?csrf-token ?handshake-data] ?data] + [{:as _msg :keys [?data]}] + (let [[_?uid _?csrf-token _?handshake-data] ?data] (debugf "Handshake: %s" ?data))) (defmethod -event-msg-handler :chsk/state - [{:keys [?data] :as ev-msg}] + [{:keys [?data] :as _msg}] (let [[old-state-map new-state-map] (have vector? ?data)] (debugf "ws state: %s" new-state-map) (rf/dispatch [:ws/state new-state-map old-state-map]))) -;; This is the main event handler; If we want to do cool things with other kinds of data -;; going back and forth, this is where we'll inject it. - (defmethod -event-msg-handler :chsk/ws-ping - [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}] + [{:as _msg :keys [event _id _?data _ring-req _?reply-fn _send-fn]}] (infof ":chsk/ws-ping: %s" event)) -(defmethod -event-msg-handler :chsk/recv - [{:as ev-msg :keys [?data]}] - (if (= ?data [:chsk/ws-ping]) - (debug "ws-ping rcvd.") - (if (vector? ?data) - (do ;(info "dispatching rcvd ws msg to reframe:" (first ?data)) - (debug "dispatching rcvd ws msg to reframe:" ?data) - (rf/dispatch ?data)) - (error "ws rcvd. cannot dispatch. data no vector: " ?data)))) - (defmethod -event-msg-handler :default - [{:as ev-msg :keys [event]}] - (errorf "Unhandled ws event: %s" event)) + [{:as _msg :keys [event _?data]}] + (infof "forwarding to reframe: %s" event) + (if (vector? event) + (do + (debug "dispatching rcvd ws msg to reframe:" event) + (rf/dispatch event)) + (error "ws rcvd. cannot dispatch. data no vector: " event))) ; msg-handler diff --git a/src/modular/ws/service.cljs b/src/modular/ws/service.cljs index 795bda9..b625903 100644 --- a/src/modular/ws/service.cljs +++ b/src/modular/ws/service.cljs @@ -1,7 +1,7 @@ (ns modular.ws.service (:require - [taoensso.timbre :as timbre :refer-macros [tracef debug debugf infof warn warnf errorf trace]] - [taoensso.sente :as sente :refer [cb-success?]] + [taoensso.timbre :as timbre :refer-macros [debug warn]] + [taoensso.sente :as sente] [taoensso.sente.packers.transit :as sente-transit] ;; Optional, for Transit encoding [modular.encoding.transit :as e] [modular.ws.msg-handler :refer [event-msg-handler]])) @@ -16,6 +16,8 @@ (debug "connecting sente websocket.. " path port) (let [packer (sente-transit/get-transit-packer :json e/encode e/decode) opts {:type :auto ; :ajax + :ws-kalive-ms 7000 + :ws-ping-timeout-ms 30000 :packer packer} opts (if port (assoc opts :port port) @@ -51,6 +53,6 @@ {:conn conn :router router})) -(defn stop-websocket-client! [{:keys [conn router] :as state}] +(defn stop-websocket-client! [{:keys [_conn router] :as _state}] (stop-router! router)) diff --git a/src/modular/ws/service/adapter.clj b/src/modular/ws/service/adapter.clj index b21f7bb..a0a14b2 100644 --- a/src/modular/ws/service/adapter.clj +++ b/src/modular/ws/service/adapter.clj @@ -3,18 +3,15 @@ [taoensso.sente.packers.transit :as sente-transit] [taoensso.sente :as sente] [modular.encoding.transit :as e] - [modular.ws.service.id :refer [get-sente-session-uid]] - [modular.ws.service.adapter.jetty :as jetty])) + [modular.ws.service.id :refer [get-sente-session-uid]])) (defn get-adapter [server-type] - (case server-type - :undertow (let [get-sch-adapter (requiring-resolve 'modular.ws.service.adapter.undertow/get-sch-adapter)] - (get-sch-adapter)) - :jetty (jetty/get-sch-adapter) - :httpkit (let [get-sch-adapter (requiring-resolve 'modular.ws.service.adapter.httpkit/get-sch-adapter)] - (get-sch-adapter)) - ; - )) + (let [s (case server-type + :untertow 'taoensso.sente.server-adapters.community.undertow/get-sch-adapter + :jetty 'taoensso.sente.server-adapters.community.jetty/get-sch-adapter + :httpkit 'taoensso.sente.server-adapters.http-kit/get-sch-adapter) + get-sch-adapter (requiring-resolve s)] + get-sch-adapter)) (defn ws-init! [server-type] (let [get-sch-adapter (get-adapter server-type) @@ -22,6 +19,8 @@ chsk-server (sente/make-channel-socket-server! (get-sch-adapter) {:packer packer + :ws-kalive-ms 15000 + :ws-ping-timeout-ms 50000 :csrf-token-fn nil ; awb99; disable CSRF checking. :user-id-fn get-sente-session-uid}) {:keys [ch-recv send-fn connected-uids diff --git a/src/modular/ws/service/adapter/httpkit.clj b/src/modular/ws/service/adapter/httpkit.clj deleted file mode 100644 index 4ec7c97..0000000 --- a/src/modular/ws/service/adapter/httpkit.clj +++ /dev/null @@ -1,7 +0,0 @@ -(ns modular.ws.service.adapter.httpkit - (:require - [taoensso.sente.server-adapters.http-kit])) - -(defn get-sch-adapter [] - (require '[taoensso.sente.server-adapters.http-kit]) - taoensso.sente.server-adapters.http-kit/get-sch-adapter) \ No newline at end of file diff --git a/src/modular/ws/service/adapter/jetty.clj b/src/modular/ws/service/adapter/jetty.clj deleted file mode 100644 index b3ef5f0..0000000 --- a/src/modular/ws/service/adapter/jetty.clj +++ /dev/null @@ -1,7 +0,0 @@ -(ns modular.ws.service.adapter.jetty - (:require - [taoensso.sente.server-adapters.jetty9])) - -(defn get-sch-adapter [] - (require '[taoensso.sente.server-adapters.jetty9]) - taoensso.sente.server-adapters.jetty9/get-sch-adapter) \ No newline at end of file diff --git a/src/modular/ws/service/adapter/undertow.clj b/src/modular/ws/service/adapter/undertow.clj deleted file mode 100644 index 1ff1849..0000000 --- a/src/modular/ws/service/adapter/undertow.clj +++ /dev/null @@ -1,7 +0,0 @@ -(ns modular.ws.service.adapter.undertow - (:require - [taoensso.sente.server-adapters.undertow])) - -(defn get-sch-adapter [] - (require '[taoensso.sente.server-adapters.undertow]) - taoensso.sente.server-adapters.undertow/get-sch-adapter) \ No newline at end of file diff --git a/src/modular/ws/service/handler.clj b/src/modular/ws/service/handler.clj index 855d1c3..38de14b 100644 --- a/src/modular/ws/service/handler.clj +++ b/src/modular/ws/service/handler.clj @@ -15,7 +15,7 @@ ; WEBSOCKET -(defn ws-token-handler-raw [req] +(defn ws-token-handler-raw [_req] (let [token {:csrf-token (get-csrf-token)}] (info "sending csrf token: " token) {:status 200 diff --git a/src/modular/ws/service/id.clj b/src/modular/ws/service/id.clj index eff67bc..2b9eb01 100644 --- a/src/modular/ws/service/id.clj +++ b/src/modular/ws/service/id.clj @@ -1,9 +1,11 @@ -(ns modular.ws.service.id) +(ns modular.ws.service.id + (:require + [nano-id.core :refer [nano-id]])) (defn unique-id "Get a unique id for a session." [] - (str (java.util.UUID/randomUUID))) + (nano-id 8)) (defn session-uid "Get session uuid from a request." @@ -13,7 +15,7 @@ (defn get-sente-session-uid "Get session uuid from a request." [req] - (or (get-in req [:session :uid]) + (or (session-uid req) (unique-id))) (defn sente-session-with-uid [req] diff --git a/src/modular/ws/service/middleware.clj b/src/modular/ws/service/middleware.clj index 29cf351..5c5d1ec 100644 --- a/src/modular/ws/service/middleware.clj +++ b/src/modular/ws/service/middleware.clj @@ -1,6 +1,4 @@ (ns modular.ws.service.middleware - "a middleware takes a handler, and wraps a middleware around it. - It is handler transformation, not routing related." (:require [clojure.string] ;[ring.util.response :refer [response]] @@ -8,10 +6,9 @@ ;[ring.middleware.anti-forgery :refer [wrap-anti-forgery]] [ring.middleware.keyword-params :refer [wrap-keyword-params]] [ring.middleware.params :refer [wrap-params]] - [ring.middleware.defaults :refer [wrap-defaults site-defaults api-defaults]] + [ring.middleware.defaults :refer [wrap-defaults site-defaults]] [ring.middleware.session :refer [wrap-session]] ; [muuntaja.middleware :refer [wrap-format]] ; 30x faster than ring.middleware.format - ; [ring.middleware.json :refer [wrap-json-response]] [modular.webserver.middleware.api :as api])) #_(defn wrap-formats2 [handler] diff --git a/src/modular/ws/ws.cljs b/src/modular/ws/ws.cljs index 9718b2a..89dd038 100644 --- a/src/modular/ws/ws.cljs +++ b/src/modular/ws/ws.cljs @@ -1,6 +1,6 @@ (ns modular.ws.ws (:require - [taoensso.timbre :as timbre :refer-macros [debug debugf info error]] + [taoensso.timbre :as timbre :refer-macros [debug debugf error]] [re-frame.core :as rf])) (defn- cb-dispatch-to-reframe