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

Binary data support #398

Closed
wants to merge 2 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
7 changes: 7 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{:deps
{org.clojure/core.async {:mvn/version "1.3.610"}
com.taoensso/encore {:mvn/version "3.12.1"}
org.java-websocket/Java-WebSocket {:mvn/version "1.5.1"}
org.clojure/tools.reader {:mvn/version "1.3.5"}
com.taoensso/timbre {:mvn/version "5.1.2"}}
}
32 changes: 10 additions & 22 deletions src/taoensso/sente.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -204,37 +204,24 @@
;; * Packing includes ->str encoding, and may incl. wrapping to carry cb info.

(defn- unpack "prefixed-pstr->[clj ?cb-uuid]"
[packer prefixed-pstr]
(have? string? prefixed-pstr)
(let [wrapped? (enc/str-starts-with? prefixed-pstr "+")
pstr (subs prefixed-pstr 1)
clj
[packer pstr]
(let [[clj ?cb-uuid]
(try
(interfaces/unpack packer pstr)
(catch #?(:clj Throwable :cljs :default) t
(debugf "Bad package: %s (%s)" pstr t)
[:chsk/bad-package pstr]))

[clj ?cb-uuid] (if wrapped? clj [clj nil])
?cb-uuid (if (= 0 ?cb-uuid) :ajax-cb ?cb-uuid)]

(tracef "Unpacking: %s -> %s" prefixed-pstr [clj ?cb-uuid])
[clj ?cb-uuid]))

(defn- pack "clj->prefixed-pstr"
([packer clj]
(let [;; "-" prefix => Unwrapped (has no callback)
pstr (str "-" (interfaces/pack packer clj))]
(tracef "Packing (unwrapped): %s -> %s" clj pstr)
pstr))
(pack packer clj nil))

([packer clj ?cb-uuid]
(let [;;; Keep wrapping as light as possible:
?cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid)
wrapped-clj (if ?cb-uuid [clj ?cb-uuid] [clj])
;; "+" prefix => Wrapped (has callback)
pstr (str "+" (interfaces/pack packer wrapped-clj))]
(tracef "Packing (wrapped): %s -> %s" wrapped-clj pstr)
(let [?cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid)
wrapped-clj [clj ?cb-uuid]
pstr (interfaces/pack packer wrapped-clj)]
pstr)))

(deftype EdnPacker []
Expand Down Expand Up @@ -688,9 +675,9 @@
handshake-ev
(if (nil? ?handshake-data) ; Micro optimization
[:chsk/handshake [uid nil]]
[:chsk/handshake [uid nil ?handshake-data]])]
(interfaces/sch-send! server-ch websocket?
(pack packer handshake-ev))))]
[:chsk/handshake [uid nil ?handshake-data]])
msg (pack packer handshake-ev)]
(interfaces/sch-send! server-ch websocket? msg)))]

(enc/cond

Expand Down Expand Up @@ -1095,6 +1082,7 @@
(aset "onerror" onerror-fn)
(aset "onmessage" onmessage-fn) ; Nb receives both push & cb evs!
;; Fires repeatedly (on each connection attempt) while server is down:
(aset "binaryType" "arraybuffer")
(aset "onclose" onclose-fn))
socket))))

Expand Down