Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick up handshake-user-data from websocket message into :chsk/state #111

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example-project/project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject com.taoensso.examples/sente "1.4.0-alpha2"
(defproject com.taoensso.examples/sente "1.4.0-SNAPSHOT"
:description "Sente, reference web-app example project"
:url "https://github.com/ptaoussanis/sente"
:license {:name "Eclipse Public License"
Expand All @@ -16,7 +16,7 @@
[org.clojure/clojurescript "0.0-2411"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]

[com.taoensso/sente "1.4.0-alpha2"] ; <--- Sente
[com.taoensso/sente "1.4.0-SNAPSHOT"] ; <--- Sente
[com.taoensso/timbre "3.3.1"]

;;; ---> Choose (uncomment) a supported web server <---
Expand Down
12 changes: 9 additions & 3 deletions example-project/src/example/my_app.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,15 @@
{:method :post
:params {:user-id (str user-id)
:csrf-token (:csrf-token @chsk-state)}}
(fn [ajax-resp] (logf "Ajax login response: %s" ajax-resp)))

(sente/chsk-reconnect! chsk)))))))
(fn [ajax-resp]
(logf "Ajax login response: %s" ajax-resp)
(let [login-successful? true ; Your logic here
]
(if-not login-successful?
(debug "Login failed")
(do
(debugf "Login successful")
(sente/chsk-reconnect! chsk))))))))))))

;;;; Example: broadcast server>user

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject com.taoensso/sente "1.4.0-alpha2"
(defproject com.taoensso/sente "1.4.0-SNAPSHOT"
:author "Peter Taoussanis <https://www.taoensso.com>"
:description "Clojure channel sockets library"
:url "https://github.com/ptaoussanis/sente"
Expand Down
36 changes: 22 additions & 14 deletions src/taoensso/sente.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Special messages:
* Callback replies: :chsk/closed, :chsk/timeout, :chsk/error.
* Client-side events:
[:chsk/handshake [<?uid> <?csrf-token>]],
[:chsk/handshake [<?uid> <?csrf-token> <?handshake-data>]],
[:chsk/ws-ping],
[:chsk/state <new-state>],
[:chsk/recv <[buffered-evs]>] ; server>user push
Expand Down Expand Up @@ -280,11 +280,12 @@
:connected-uids ; Watchable, read-only (atom {:ws #{_} :ajax #{_} :any #{_}}).

Common options:
:user-id-fn ; (fn [ring-req]) -> unique user-id for server>user push.
:csrf-token-fn ; (fn [ring-req]) -> CSRF token for Ajax POSTs.
:send-buf-ms-ajax ; [2]
:send-buf-ms-ws ; [2]
:packer ; :edn (default), or an IPacker implementation (experimental).
:user-id-fn ; (fn [ring-req]) -> unique user-id for server>user push.
:csrf-token-fn ; (fn [ring-req]) -> CSRF token for Ajax POSTs.
:handshake-data-fn ; (fn [ring-req]) -> arb data to append to handshake evs.
:send-buf-ms-ajax ; [2]
:send-buf-ms-ws ; [2]
:packer ; :edn (default), or an IPacker implementation (experimental).

[1] e.g. `taoensso.sente.server-adapters.http-kit/http-kit-adapter` or
`taoensso.sente.server-adapters.immutant/immutant-adapter`.
Expand All @@ -298,7 +299,7 @@

[web-server-adapter ; Actually a net-ch-adapter, but that may be confusing
& [{:keys [recv-buf-or-n send-buf-ms-ajax send-buf-ms-ws
user-id-fn csrf-token-fn packer]
user-id-fn csrf-token-fn handshake-data-fn packer]
:or {recv-buf-or-n (async/sliding-buffer 1000)
send-buf-ms-ajax 100
send-buf-ms-ws 30
Expand All @@ -307,6 +308,7 @@
(or (get-in ring-req [:session :csrf-token])
(get-in ring-req [:session :ring.middleware.anti-forgery/anti-forgery-token])
(get-in ring-req [:session "__anti-forgery-token"])))
handshake-data-fn (fn [ring-req] nil)
packer :edn}}]]

{:pre [(have? enc/pos-int? send-buf-ms-ajax send-buf-ms-ws)
Expand Down Expand Up @@ -505,9 +507,14 @@
handshake!
(fn [net-ch]
(tracef "Handshake!")
(interfaces/send! net-ch
(pack packer nil [:chsk/handshake [uid csrf-token]])
(not websocket?)))]
(let [?handshake-user-data (handshake-data-fn ring-req)
handshake-ev
(if-not (nil? ?handshake-user-data)
[:chsk/handshake [uid csrf-token ?handshake-user-data]]
[:chsk/handshake [uid csrf-token]])]
(interfaces/send! net-ch
(pack packer nil handshake-ev)
(not websocket?))))]

(if (str/blank? client-id)
(let [err-msg "Client's Ring request doesn't have a client id. Does your server have the necessary keyword Ring middleware (`wrap-params` & `wrap-keyword-params`)?"]
Expand Down Expand Up @@ -745,13 +752,14 @@
(tracef "handle-when-handshake!: %s" clj)
(when (and (vector? clj) ; Nb clj may be callback reply
(= (first clj) :chsk/handshake))
(let [[_ [uid csrf-token]] clj]
(let [[_ [uid csrf-token ?handshake-user-data]] clj]
(when (str/blank? csrf-token)
(warnf "NO CSRF TOKEN AVAILABLE FOR USE BY SENTE"))
(merge>chsk-state! chsk
{:open? true
:uid uid
:csrf-token csrf-token})
{:open? true
:uid uid
:csrf-token csrf-token
:?handshake-user-data ?handshake-user-data})
:handled)))

#+cljs
Expand Down