diff --git a/deps.edn b/deps.edn index e503fab..4baf65f 100644 --- a/deps.edn +++ b/deps.edn @@ -6,7 +6,8 @@ ring/ring-anti-forgery {:mvn/version "1.3.0"} org.pinkgorilla/modular {:mvn/version "0.0.39"} ; encoding org.pinkgorilla/permission {:mvn/version "0.0.15"} - org.pinkgorilla/webserver {:mvn/version "0.0.10"}} + org.pinkgorilla/webserver {:mvn/version "0.0.10"} ; middleware + } :aliases {:outdated {:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"}} diff --git a/resources/ext/ws.edn b/resources/ext/ws.edn index e392eed..83cd19c 100644 --- a/resources/ext/ws.edn +++ b/resources/ext/ws.edn @@ -3,5 +3,5 @@ :lazy false :cljs-namespace [modular.ws.core] :cljs-ns-bindings {'modular.ws.core {'send! modular.ws.core/send! - 'init-ws! modular.ws.core/init-ws! + 'start-websocket-client! modular.ws.core/start-websocket-client! }}} \ No newline at end of file diff --git a/src/modular/ws/core.clj b/src/modular/ws/core.clj index c61a263..0c86d24 100644 --- a/src/modular/ws/core.clj +++ b/src/modular/ws/core.clj @@ -1,46 +1,43 @@ (ns modular.ws.core (:require [taoensso.timbre :as log :refer [error info warn]] - [modular.ws.adapter :refer [ws-init! start-router!]] - [modular.ws.handler :refer [add-ws-handler]] - [modular.ws.ws :as ws])) + [modular.ws.ws :as ws] + [modular.ws.service :as service])) -(def c (atom nil)) +(defonce state-a (atom nil)) -(def watcher-cbs (atom [])) +(defn start-websocket-server [server-type sente-debug?] + (let [state (service/start-websocket-server server-type sente-debug?)] + (reset! state-a state) + state)) -(defn- watch-conn-start [conn] - (let [{:keys [connected-uids]} conn] - (add-watch connected-uids :connected-uids - (fn [_ _ old new] - (when (not= old new) - (doall (for [cb @watcher-cbs] - (cb old new)))))))) +(defn stop-websocket-server [state] + (service/stop-websocket-server state) + (reset! state-a nil) + nil) -(defn watch-conn [cb] - (swap! watcher-cbs conj cb)) - -(defn- log-conn-chg [old new] - (info "conn chg: old:" old "new: " new)) -(defn init-ws! [server-type] - (let [conn (ws-init! server-type)] - (reset! c conn) - (watch-conn log-conn-chg) - (watch-conn-start conn) - (start-router! conn) - (add-ws-handler conn))) +(defn watch-conn [cb] + (swap! (:watch @state-a) conj cb)) (defn send! [uid data] - (if @c - (ws/send! @c uid data) + (if-let [conn (:conn @state-a)] + (ws/send! conn uid data) (error "ws/send - not setup. data: " data))) (defn send-all! [data] - (if @c - (ws/send-all! @c data) + (if-let [conn (:conn @state-a)] + (ws/send-all! conn data) (error "ws/send-all - not setup. data: " data))) +(defn connected-uids [] + (let [conn (:conn @state-a) + {:keys [connected-uids]} conn + uids (:any @connected-uids)] + uids)) + + + (defn send-response [{:as ev-msg :keys [id ?data ring-req ?reply-fn uid send-fn]} msg-type response] ;(let [session (:session ring-req) @@ -59,10 +56,7 @@ :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))) -(defn connected-uids [] - (let [{:keys [connected-uids]} @c - uids (:any @connected-uids)] - uids)) + (comment ;(println "clients: " @connected-uids) diff --git a/src/modular/ws/core.cljs b/src/modular/ws/core.cljs index 5a982dc..0011d16 100644 --- a/src/modular/ws/core.cljs +++ b/src/modular/ws/core.cljs @@ -2,29 +2,35 @@ (:require [taoensso.timbre :refer-macros [debug info error]] [re-frame.core :as rf] - [modular.ws.adapter :refer [ws-init! start-router!]] + [modular.ws.service :as service] [modular.ws.ws :as ws])) -(defonce c (atom nil)) +(defonce state (atom nil)) + +(defn start-websocket-client! [path port] + (let [s (service/start-websocket-client! path port)] + (reset! state s) + s)) + +(defn stop-websocket-client! [state] + (service/stop-websocket-client! state) + (reset! state nil) + nil) -(defn init-ws! [path port] - (let [conn (ws-init! path port)] - (reset! c conn) - (start-router! conn))) (defn send! ([data] (when data (info "sending (no cb): " data) (try - (ws/send @c data) + (ws/send (:conn @state) data) (catch :default e (error "exception sending to ws: " e))))) ([data cb timeout] (when data (info "sending (cb): " data) (try - (ws/send @c data cb timeout) + (ws/send (:conn @state) data cb timeout) (catch :default e (error "exception sending to ws: " e)))))) diff --git a/src/modular/ws/events.cljs b/src/modular/ws/events.cljs index 0968c68..68860e0 100644 --- a/src/modular/ws/events.cljs +++ b/src/modular/ws/events.cljs @@ -3,7 +3,7 @@ [taoensso.timbre :refer-macros [debug info warn error]] [cemerick.url :as url] [re-frame.core :as rf] - [modular.ws.core :refer [init-ws!]])) + [modular.ws.core :refer [start-websocket-client!]])) (defn application-url "gets the current url, as a map" @@ -34,7 +34,7 @@ (fn [db [_]] (let [port (changed-port db)] (warn "ws connect to port (nil=unchanged): " port) - (init-ws! "/api/chsk" port) + (start-websocket-client! "/api/chsk" port) db))) #_[{:type :auto diff --git a/src/modular/ws/service.clj b/src/modular/ws/service.clj new file mode 100644 index 0000000..8e6b747 --- /dev/null +++ b/src/modular/ws/service.clj @@ -0,0 +1,22 @@ +(ns modular.ws.service + (:require + [taoensso.sente :as sente] + [modular.ws.service.adapter :as adapter] + [modular.ws.service.handler :as handler] + [modular.ws.service.router :as router] + [modular.ws.service.watch :as watch])) + +(defn start-websocket-server [server-type sente-debug?] + (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 state}] + (router/stop-router! router)) \ No newline at end of file diff --git a/src/modular/ws/adapter.cljs b/src/modular/ws/service.cljs similarity index 79% rename from src/modular/ws/adapter.cljs rename to src/modular/ws/service.cljs index a3497c3..4897ec0 100644 --- a/src/modular/ws/adapter.cljs +++ b/src/modular/ws/service.cljs @@ -1,4 +1,4 @@ -(ns modular.ws.adapter +(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?]] @@ -31,9 +31,6 @@ ; router -(defonce router_ (atom nil)) - -(defn stop-router! [] (when-let [stop-f @router_] (stop-f))) (defn sente-csrf-warning [] (if ?csrf-token @@ -42,10 +39,20 @@ (defn start-router! [conn] (let [{:keys [ch-chsk]} conn] - (stop-router!) (sente-csrf-warning) - (reset! router_ - (sente/start-client-chsk-router! ch-chsk event-msg-handler)))) + (sente/start-client-chsk-router! ch-chsk event-msg-handler))) + +(defn stop-router! [stop-f] + (when stop-f + (stop-f))) + +(defn start-websocket-client! [path port] + (let [conn (ws-init! path port) + router (start-router! conn)] + {:conn conn + :router router})) +(defn stop-websocket-client! [{:keys [conn router] :as state}] + (stop-router! router)) diff --git a/src/modular/ws/adapter.clj b/src/modular/ws/service/adapter.clj similarity index 67% rename from src/modular/ws/adapter.clj rename to src/modular/ws/service/adapter.clj index 8ab32c6..1b91d5d 100644 --- a/src/modular/ws/adapter.clj +++ b/src/modular/ws/service/adapter.clj @@ -1,25 +1,22 @@ -(ns modular.ws.adapter +(ns modular.ws.service.adapter (:require [taoensso.timbre :as log :refer [debug info infof]] [taoensso.sente.packers.transit :as sente-transit] [taoensso.sente :as sente] [modular.encoding.transit :as e] - [modular.ws.id :refer [get-sente-session-uid]] - [modular.ws.msg-handler :refer [event-msg-handler]] + [modular.ws.service.id :refer [get-sente-session-uid]] [modular.ws.adapter.jetty :as jetty])) (defn get-adapter [server-type] (case server-type - :undertow (let [get-sch-adapter (requiring-resolve 'modular.ws.adapter.undertow/get-sch-adapter)] + :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.adapter.httpkit/get-sch-adapter)] + :httpkit (let [get-sch-adapter (requiring-resolve 'modular.ws.service.adapter.httpkit/get-sch-adapter)] (get-sch-adapter)) ; )) -(reset! sente/debug-mode?_ true) ; Uncomment for extra debug info - (defn ws-init! [server-type] (let [get-sch-adapter (get-adapter server-type) packer (sente-transit/get-transit-packer :json e/encode e/decode) @@ -34,17 +31,4 @@ :ch-chsk ch-recv :connected-uids connected-uids :ring-ajax-get-or-ws-handshake ajax-get-or-ws-handshake-fn - :ring-ajax-post ajax-post-fn})) - -; router - -(defonce router_ (atom nil)) - -(defn stop-router! [] - (when-let [stop-fn @router_] (stop-fn))) - -(defn start-router! [conn] - (let [{:keys [ch-chsk]} conn] - (stop-router!) - (reset! router_ - (sente/start-server-chsk-router! ch-chsk event-msg-handler)))) + :ring-ajax-post ajax-post-fn})) \ No newline at end of file diff --git a/src/modular/ws/adapter/httpkit.clj b/src/modular/ws/service/adapter/httpkit.clj similarity index 88% rename from src/modular/ws/adapter/httpkit.clj rename to src/modular/ws/service/adapter/httpkit.clj index b135402..0a13db0 100644 --- a/src/modular/ws/adapter/httpkit.clj +++ b/src/modular/ws/service/adapter/httpkit.clj @@ -1,4 +1,4 @@ -(ns modular.ws.adapter.httpkit +(ns modular.ws.service.adapter.httpkit (:require [taoensso.timbre :as log :refer [debug info infof]] [taoensso.sente.server-adapters.http-kit])) diff --git a/src/modular/ws/adapter/jetty.clj b/src/modular/ws/service/adapter/jetty.clj similarity index 88% rename from src/modular/ws/adapter/jetty.clj rename to src/modular/ws/service/adapter/jetty.clj index dbcfb09..11f055d 100644 --- a/src/modular/ws/adapter/jetty.clj +++ b/src/modular/ws/service/adapter/jetty.clj @@ -1,4 +1,4 @@ -(ns modular.ws.adapter.jetty +(ns modular.ws.service.adapter.jetty (:require [taoensso.timbre :as log :refer [debug info infof]] [taoensso.sente.server-adapters.jetty9])) diff --git a/src/modular/ws/adapter/undertow.clj b/src/modular/ws/service/adapter/undertow.clj similarity index 87% rename from src/modular/ws/adapter/undertow.clj rename to src/modular/ws/service/adapter/undertow.clj index 10d6f41..6d72569 100644 --- a/src/modular/ws/adapter/undertow.clj +++ b/src/modular/ws/service/adapter/undertow.clj @@ -1,4 +1,4 @@ -(ns modular.ws.adapter.undertow +(ns modular.ws.service.adapter.undertow (:require [taoensso.timbre :as log :refer [debug info infof]] [taoensso.sente.server-adapters.undertow])) diff --git a/src/modular/ws/handler.clj b/src/modular/ws/service/handler.clj similarity index 67% rename from src/modular/ws/handler.clj rename to src/modular/ws/service/handler.clj index cd3c042..3ee381f 100644 --- a/src/modular/ws/handler.clj +++ b/src/modular/ws/service/handler.clj @@ -1,11 +1,10 @@ -(ns modular.ws.handler +(ns modular.ws.service.handler (:require [taoensso.timbre :as log :refer [debug debugf info infof]] [cheshire.core :as json] [ring.middleware.anti-forgery :refer [*anti-forgery-token*]] - [modular.webserver.handler.registry :refer [add-ring-handler]] - [modular.ws.id :refer [get-sente-session-uid]] - [modular.ws.middleware :refer [wrap-ws]])) + [modular.ws.service.id :refer [get-sente-session-uid]] + [modular.ws.service.middleware :refer [wrap-ws]])) ; CSRF TOKEN @@ -42,13 +41,7 @@ ;(info "ws csrf: " (get-in req [:session :ring.middleware.anti-forgery/anti-forgery-token])) res)) -(def ws-token-handler-raw-wrapped (wrap-ws ws-token-handler-raw)) -;(def ws-handshake-handler-wrapped (wrap-ws ws-handshake-handler)) -;(def ws-ajax-post-handler-wrapped (wrap-ws ws-ajax-post-handler)) - - -(defn add-ws-handler [conn] - (add-ring-handler :ws/token (wrap-ws ws-token-handler-raw)) - (add-ring-handler :ws/chsk-get (wrap-ws (partial ws-handshake-handler conn))) - (add-ring-handler :ws/chsk-post (wrap-ws (partial ws-ajax-post-handler conn)))) - +(defn create-bidi-routes [conn] + {"token" (wrap-ws ws-token-handler-raw) + "chsk" {:get (wrap-ws (partial ws-handshake-handler conn)) + :post (wrap-ws (partial ws-ajax-post-handler conn))}}) diff --git a/src/modular/ws/id.clj b/src/modular/ws/service/id.clj similarity index 94% rename from src/modular/ws/id.clj rename to src/modular/ws/service/id.clj index 9ba9152..eff67bc 100644 --- a/src/modular/ws/id.clj +++ b/src/modular/ws/service/id.clj @@ -1,4 +1,4 @@ -(ns modular.ws.id) +(ns modular.ws.service.id) (defn unique-id "Get a unique id for a session." diff --git a/src/modular/ws/middleware.clj b/src/modular/ws/service/middleware.clj similarity index 98% rename from src/modular/ws/middleware.clj rename to src/modular/ws/service/middleware.clj index 8d0598a..0315b47 100644 --- a/src/modular/ws/middleware.clj +++ b/src/modular/ws/service/middleware.clj @@ -1,4 +1,4 @@ -(ns modular.ws.middleware +(ns modular.ws.service.middleware "a middleware takes a handler, and wraps a middleware around it. It is handler transformation, not routing related." (:require diff --git a/src/modular/ws/service/router.clj b/src/modular/ws/service/router.clj new file mode 100644 index 0000000..82d3d52 --- /dev/null +++ b/src/modular/ws/service/router.clj @@ -0,0 +1,14 @@ +(ns modular.ws.service.router + (:require + [taoensso.sente :as sente] + [modular.ws.msg-handler :refer [event-msg-handler]])) + +; router + +(defn stop-router! [stop-fn] + (when stop-fn + (stop-fn))) + +(defn start-router! [conn] + (let [{:keys [ch-chsk]} conn] + (sente/start-server-chsk-router! ch-chsk event-msg-handler))) diff --git a/src/modular/ws/service/watch.clj b/src/modular/ws/service/watch.clj new file mode 100644 index 0000000..8651fbd --- /dev/null +++ b/src/modular/ws/service/watch.clj @@ -0,0 +1,14 @@ +(ns modular.ws.service.watch) + + +(defn watch-conn-start [conn] + (let [watcher-cbs (atom []) + {:keys [connected-uids]} conn] + (add-watch connected-uids :connected-uids + (fn [_ _ old new] + (when (not= old new) + (doall (for [cb @watcher-cbs] + (cb old new)))))) + watcher-cbs + )) +