Skip to content

Commit

Permalink
websocket https protocol added
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Jan 5, 2025
1 parent 320906b commit 056b82d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
35 changes: 24 additions & 11 deletions spa/src/webly/spa/service/ws.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
(url/url (-> js/window .-location .-href)))

(defn server-port []
(-> (application-url) :port))
(let [port (-> (application-url) :port)]
(info "detected server port: " port)
port))

(defn is-https? []
(let [{:keys [protocol]} (application-url)]
Expand All @@ -21,17 +23,28 @@
(defn start-ws [{:keys [webly-http-port shadow-dev-http-port] :as ports}]
; sente websockets dont work on shadow-dev-http server.
; therefore we use webly-http port when browser uses shadow-dev-http
(let [port (server-port)
served-by-shadow-dev? (= port shadow-dev-http-port)
port (if served-by-shadow-dev?
webly-http-port
port)
route "/api/chsk"]
(if served-by-shadow-dev?
(info "this is a shadow-cljs-dev session. connecting websocket to WEBLY SERVER route: " route " port: " port)
(info "connecting websocket route: " route " port: " port))
(let [protocol (if (is-https?)
:https
:http)
route "/api/chsk"
detected-port (server-port)
port (cond
; https always on 443
(is-https?)
443

; if the port matches the shadow-dev-http port, we are on shadow-dev, so we redirect
(= detected-port shadow-dev-http-port)
(do
(info "this is a shadow-cljs-dev session. connecting websocket to WEBLY SERVER on port: " shadow-dev-http-port)
webly-http-port)

; in all other cases we use the detected port
:else
detected-port)]
(if port
(start-websocket-client! route port)
(do (info "connecting websocket protocol: " protocol "route: " route " port: " port)
(start-websocket-client! protocol route port))
(error "WEBSOCKET cannot connect. port nil! webly-http-port: " webly-http-port " shadow-port: " shadow-dev-http-port))))


2 changes: 1 addition & 1 deletion webly/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
org.pinkgorilla/extension {:mvn/version "0.0.14"} ; brings modular
org.pinkgorilla/webserver {:mvn/version "0.0.13"}
;org.pinkgorilla/webserver {:local/root "../../webserver" :deps/manifest :deps}
org.pinkgorilla/websocket {:mvn/version "0.0.23"}
org.pinkgorilla/websocket {:mvn/version "0.0.24"}
;org.pinkgorilla/websocket {:local/root "../../websocket" :deps/manifest :deps}
org.pinkgorilla/spa {:local/root "../spa" :deps/manifest :deps}}
:aliases
Expand Down

0 comments on commit 056b82d

Please sign in to comment.