Skip to content

Commit

Permalink
sente mega refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Nov 26, 2024
1 parent 1a3d8d7 commit 4cc9daf
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 153 deletions.
7 changes: 4 additions & 3 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/modular/ws/core.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
(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]
[modular.ws.service.watch :as watch]))

;; 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
Expand Down
11 changes: 9 additions & 2 deletions src/modular/ws/core.cljs
Original file line number Diff line number Diff line change
@@ -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]))
Expand Down Expand Up @@ -44,4 +44,11 @@
(info "ws send (cb): " data)
(send! data cb timeout))
(error ":ws/send bad format: " v))
db))
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

61 changes: 3 additions & 58 deletions src/modular/ws/events.cljs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
49 changes: 29 additions & 20 deletions src/modular/ws/msg_handler.clj
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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"
Expand Down
34 changes: 13 additions & 21 deletions src/modular/ws/msg_handler.cljs
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 5 additions & 3 deletions src/modular/ws/service.cljs
Original file line number Diff line number Diff line change
@@ -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]]))
Expand All @@ -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)
Expand Down Expand Up @@ -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))

19 changes: 9 additions & 10 deletions src/modular/ws/service/adapter.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
[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)
packer (sente-transit/get-transit-packer :json e/encode e/decode)
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
Expand Down
7 changes: 0 additions & 7 deletions src/modular/ws/service/adapter/httpkit.clj

This file was deleted.

7 changes: 0 additions & 7 deletions src/modular/ws/service/adapter/jetty.clj

This file was deleted.

7 changes: 0 additions & 7 deletions src/modular/ws/service/adapter/undertow.clj

This file was deleted.

2 changes: 1 addition & 1 deletion src/modular/ws/service/handler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/modular/ws/service/id.clj
Original file line number Diff line number Diff line change
@@ -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."
Expand All @@ -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]
Expand Down
Loading

0 comments on commit 4cc9daf

Please sign in to comment.