|
20 | 20 | * cb - Callback |
21 | 21 | * tout - Timeout |
22 | 22 | * ws - WebSocket/s |
23 | | - * pstr - Packed string. Arbitrary Clojure data serialized as a |
24 | | - string (e.g. edn) for client<->server comms |
| 23 | + * packed - Arbitrary Clojure value serialized for client<->server comms |
25 | 24 | * udt - Unix timestamp (datetime long) |
26 | 25 |
|
27 | 26 | Special messages: |
|
203 | 202 | ;; * Client<->server payloads are arbitrary Clojure vals (cb replies or events). |
204 | 203 | ;; * Payloads are packed for client<->server transit. |
205 | 204 |
|
206 | | -(defn- unpack "packed->[clj ?cb-uuid]" |
| 205 | +(defn- unpack |
| 206 | + "packed->[clj ?cb-uuid]" |
207 | 207 | [packer packed] |
208 | 208 | (let [[clj ?cb-uuid] |
209 | 209 | (try |
|
216 | 216 |
|
217 | 217 | [clj ?cb-uuid])) |
218 | 218 |
|
219 | | -(defn- pack "[clj ?cb-uuid]->packed" |
| 219 | +(defn- pack |
| 220 | + "[clj ?cb-uuid]->packed" |
220 | 221 | ([packer clj ] (pack packer clj nil)) |
221 | 222 | ([packer clj ?cb-uuid] |
222 | | - (let [?cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid)] |
223 | | - (interfaces/pack packer |
224 | | - (if-some [cb-uuid ?cb-uuid] |
225 | | - [clj cb-uuid] |
226 | | - [clj ]))))) |
| 223 | + (interfaces/pack packer |
| 224 | + (if-some [cb-uuid (if (= ?cb-uuid :ajax-cb) 0 ?cb-uuid)] |
| 225 | + [clj cb-uuid] |
| 226 | + [clj ])))) |
227 | 227 |
|
228 | 228 | (comment |
229 | 229 | (unpack default-edn-packer |
|
517 | 517 | (have? vector? buffered-evs) |
518 | 518 | (have? set? ev-uuids) |
519 | 519 |
|
520 | | - (let [buffered-evs-ppstr (pack packer buffered-evs)] |
| 520 | + (let [packed-buffered-evs (pack packer buffered-evs)] |
521 | 521 | (send-buffered-server-evs>clients! conn-type |
522 | | - conns_ uid buffered-evs-ppstr (count buffered-evs))))))] |
| 522 | + conns_ uid packed-buffered-evs (count buffered-evs))))))] |
523 | 523 |
|
524 | 524 | (if (= ev [:chsk/close]) ; Currently undocumented |
525 | 525 | (do |
|
643 | 643 | (fn [server-ch websocket?] |
644 | 644 | (assert (not websocket?)) |
645 | 645 | (let [params (get ring-req :params) |
646 | | - ppstr (get params :ppstr) |
| 646 | + packed (or (get params :packed) (get params :ppstr)) |
647 | 647 | client-id (get params :client-id) |
648 | | - [clj has-cb?] (unpack packer ppstr) |
| 648 | + [clj has-cb?] (unpack packer packed) |
649 | 649 | reply-fn |
650 | 650 | (let [replied?_ (atom false)] |
651 | 651 | (fn [resp-clj] ; Any clj form |
|
727 | 727 | lid* error)) |
728 | 728 |
|
729 | 729 | on-msg |
730 | | - (fn [server-ch websocket? req-ppstr] |
| 730 | + (fn [server-ch websocket? packed] |
731 | 731 | (assert websocket?) |
732 | 732 | (swap-in! conns_ [:ws uid client-id] |
733 | 733 | (fn [[?sch _udt conn-id]] |
734 | 734 | (when conn-id [?sch (enc/now-udt) conn-id]))) |
735 | 735 |
|
736 | | - (let [[clj ?cb-uuid] (unpack packer req-ppstr)] |
| 736 | + (let [[clj ?cb-uuid] (unpack packer packed)] |
737 | 737 | ;; clj should be ev |
738 | 738 | (cond |
739 | 739 | (= clj [:chsk/ws-pong]) (receive-event-msg! clj nil) |
|
938 | 938 | (defn- send-buffered-server-evs>clients! |
939 | 939 | "Actually pushes buffered events (as packed-str) to all uid's conns. |
940 | 940 | Allows some time for possible reconnects." |
941 | | - [conn-type conns_ uid buffered-evs-pstr n-buffered-evs] |
| 941 | + [conn-type conns_ uid packed-buffered-evs n-buffered-evs] |
942 | 942 | (have? [:el #{:ajax :ws}] conn-type) |
943 | 943 | (let [;; Mean max wait time: sum*1.5 = 2790*1.5 = 4.2s |
944 | 944 | ms-backoffs [90 180 360 720 720 720] ; => max 1+6 attempts |
|
955 | 955 | (when-let [[?sch _udt conn-id] (get-in @conns_ [conn-type uid client-id])] |
956 | 956 | (when-let [sch ?sch] |
957 | 957 | (when-not (simulated-bad-conn?) |
958 | | - (when (interfaces/sch-send! sch websocket? buffered-evs-pstr) |
| 958 | + (when (interfaces/sch-send! sch websocket? packed-buffered-evs) |
959 | 959 | conn-id))))] |
960 | 960 |
|
961 | 961 | (swap-in! conns_ [conn-type uid client-id] |
|
1359 | 1359 |
|
1360 | 1360 | ;; TODO Buffer before sending (but honor `:flush?`) |
1361 | 1361 | (let [?cb-uuid (when ?cb-fn (enc/uuid-str 6)) |
1362 | | - ppstr (pack packer ev ?cb-uuid)] |
| 1362 | + packed (pack packer ev ?cb-uuid)] |
1363 | 1363 |
|
1364 | 1364 | (when-let [cb-uuid ?cb-uuid] |
1365 | 1365 | (reset-in! cbs-waiting_ [cb-uuid] (have ?cb-fn)) |
|
1372 | 1372 | (or |
1373 | 1373 | (when-let [[s _sid] @socket_] |
1374 | 1374 | (try |
1375 | | - #?(:cljs (.send s ppstr) |
1376 | | - :clj (.send ^WebSocketClient s ^String ppstr)) |
| 1375 | + #?(:cljs (.send s packed) |
| 1376 | + :clj (.send ^WebSocketClient s packed)) |
1377 | 1377 |
|
1378 | 1378 | (reset! udt-last-comms_ (enc/now-udt)) |
1379 | 1379 | :apparent-success |
|
1435 | 1435 | {:udt (enc/now-udt), :ex ex}))))) |
1436 | 1436 |
|
1437 | 1437 | on-message ; Nb receives both push & cb evs! |
1438 | | - (fn #?(:cljs [ws-ev] :clj [ppstr]) |
1439 | | - (let [ppstr #?(:clj ppstr |
1440 | | - :cljs (enc/oget ws-ev "data")) |
1441 | | - |
1442 | | - ;; `clj` may/not satisfy `event?` since |
1443 | | - ;; we also receive cb replies here. This |
1444 | | - ;; is why we prefix pstrs to indicate |
1445 | | - ;; whether they're wrapped or not |
1446 | | - [clj ?cb-uuid] (unpack packer ppstr)] |
| 1438 | + (fn #?(:cljs [ws-ev] :clj [packed]) |
| 1439 | + (let [#?@(:cljs [packed (enc/oget ws-ev "data")]) |
| 1440 | + ;; `clj` may/not satisfy `event?` since we also receive cb replies here |
| 1441 | + [clj ?cb-uuid] (unpack packer packed)] |
1447 | 1442 |
|
1448 | 1443 | (reset! udt-last-comms_ (enc/now-udt)) |
1449 | 1444 |
|
|
1615 | 1610 | {:method :post |
1616 | 1611 | :timeout-ms (or ?timeout-ms (:timeout-ms ajax-opts) |
1617 | 1612 | default-client-side-ajax-timeout-ms) |
1618 | | - :resp-type :text ; We'll do our own pstr decoding |
| 1613 | + :resp-type :text ; TODO Support binary? |
1619 | 1614 | :headers |
1620 | 1615 | (merge |
1621 | 1616 | (:headers ajax-opts) ; 1st (don't clobber impl.) |
1622 | 1617 | {:X-CSRF-Token csrf-token-str}) |
1623 | 1618 |
|
1624 | 1619 | :params |
1625 | | - (let [ppstr (pack packer ev (when ?cb-fn :ajax-cb))] |
| 1620 | + (let [packed (pack packer ev (when ?cb-fn :ajax-cb))] |
1626 | 1621 | (merge params ; 1st (don't clobber impl.): |
1627 | 1622 | {:udt (enc/now-udt) ; Force uncached resp |
1628 | 1623 |
|
|
1635 | 1630 | ;; implementation: |
1636 | 1631 | :client-id client-id |
1637 | 1632 |
|
1638 | | - :ppstr ppstr}))}) |
| 1633 | + :packed packed}))}) |
1639 | 1634 |
|
1640 | 1635 | (fn ajax-cb [{:keys [?error ?content]}] |
1641 | 1636 | (if ?error |
|
1645 | 1640 | (swap-chsk-state! chsk #(chsk-state->closed % :unexpected)) |
1646 | 1641 | (when ?cb-fn (?cb-fn :chsk/error)))) |
1647 | 1642 |
|
1648 | | - (let [content ?content |
1649 | | - resp-ppstr content |
1650 | | - [resp-clj _] (unpack packer resp-ppstr)] |
| 1643 | + (let [packed ?content |
| 1644 | + [resp-clj _] (unpack packer packed)] |
1651 | 1645 | (if ?cb-fn |
1652 | 1646 | (?cb-fn resp-clj) |
1653 | 1647 | (when (not= resp-clj :chsk/dummy-cb-200) |
|
1678 | 1672 | {:method :get ; :timeout-ms timeout-ms |
1679 | 1673 | :timeout-ms (or (:timeout-ms ajax-opts) |
1680 | 1674 | default-client-side-ajax-timeout-ms) |
1681 | | - :resp-type :text ; Prefer to do our own pstr reading |
| 1675 | + :resp-type :text ; TODO Support binary? |
1682 | 1676 | :xhr-cb-fn (fn [xhr] (reset! curr-xhr_ xhr)) |
1683 | 1677 | :params |
1684 | 1678 | (merge |
|
1714 | 1708 | (retry-fn))) |
1715 | 1709 |
|
1716 | 1710 | ;; The Ajax long-poller is used only for events, never cbs: |
1717 | | - (let [content ?content |
1718 | | - ppstr content |
1719 | | - [clj] (unpack packer ppstr) |
| 1711 | + (let [packed ?content |
| 1712 | + [clj] (unpack packer packed) |
1720 | 1713 | handshake? (handshake? clj)] |
1721 | 1714 |
|
1722 | 1715 | (when handshake? |
|
0 commit comments