|
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: |
|
517 | 516 | (have? vector? buffered-evs) |
518 | 517 | (have? set? ev-uuids) |
519 | 518 |
|
520 | | - (let [buffered-evs-ppstr (pack packer buffered-evs)] |
| 519 | + (let [packed-buffered-evs (pack packer buffered-evs)] |
521 | 520 | (send-buffered-server-evs>clients! conn-type |
522 | | - conns_ uid buffered-evs-ppstr (count buffered-evs))))))] |
| 521 | + conns_ uid packed-buffered-evs (count buffered-evs))))))] |
523 | 522 |
|
524 | 523 | (if (= ev [:chsk/close]) ; Currently undocumented |
525 | 524 | (do |
|
643 | 642 | (fn [server-ch websocket?] |
644 | 643 | (assert (not websocket?)) |
645 | 644 | (let [params (get ring-req :params) |
646 | | - ppstr (get params :ppstr) |
| 645 | + packed (or (get params :packed) (get params :ppstr)) |
647 | 646 | client-id (get params :client-id) |
648 | | - [clj has-cb?] (unpack packer ppstr) |
| 647 | + [clj has-cb?] (unpack packer packed) |
649 | 648 | reply-fn |
650 | 649 | (let [replied?_ (atom false)] |
651 | 650 | (fn [resp-clj] ; Any clj form |
|
727 | 726 | lid* error)) |
728 | 727 |
|
729 | 728 | on-msg |
730 | | - (fn [server-ch websocket? req-ppstr] |
| 729 | + (fn [server-ch websocket? packed] |
731 | 730 | (assert websocket?) |
732 | 731 | (swap-in! conns_ [:ws uid client-id] |
733 | 732 | (fn [[?sch _udt conn-id]] |
734 | 733 | (when conn-id [?sch (enc/now-udt) conn-id]))) |
735 | 734 |
|
736 | | - (let [[clj ?cb-uuid] (unpack packer req-ppstr)] |
| 735 | + (let [[clj ?cb-uuid] (unpack packer packed)] |
737 | 736 | ;; clj should be ev |
738 | 737 | (cond |
739 | 738 | (= clj [:chsk/ws-pong]) (receive-event-msg! clj nil) |
|
938 | 937 | (defn- send-buffered-server-evs>clients! |
939 | 938 | "Actually pushes buffered events (as packed-str) to all uid's conns. |
940 | 939 | Allows some time for possible reconnects." |
941 | | - [conn-type conns_ uid buffered-evs-pstr n-buffered-evs] |
| 940 | + [conn-type conns_ uid packed-buffered-evs n-buffered-evs] |
942 | 941 | (have? [:el #{:ajax :ws}] conn-type) |
943 | 942 | (let [;; Mean max wait time: sum*1.5 = 2790*1.5 = 4.2s |
944 | 943 | ms-backoffs [90 180 360 720 720 720] ; => max 1+6 attempts |
|
955 | 954 | (when-let [[?sch _udt conn-id] (get-in @conns_ [conn-type uid client-id])] |
956 | 955 | (when-let [sch ?sch] |
957 | 956 | (when-not (simulated-bad-conn?) |
958 | | - (when (interfaces/sch-send! sch websocket? buffered-evs-pstr) |
| 957 | + (when (interfaces/sch-send! sch websocket? packed-buffered-evs) |
959 | 958 | conn-id))))] |
960 | 959 |
|
961 | 960 | (swap-in! conns_ [conn-type uid client-id] |
|
1359 | 1358 |
|
1360 | 1359 | ;; TODO Buffer before sending (but honor `:flush?`) |
1361 | 1360 | (let [?cb-uuid (when ?cb-fn (enc/uuid-str 6)) |
1362 | | - ppstr (pack packer ev ?cb-uuid)] |
| 1361 | + packed (pack packer ev ?cb-uuid)] |
1363 | 1362 |
|
1364 | 1363 | (when-let [cb-uuid ?cb-uuid] |
1365 | 1364 | (reset-in! cbs-waiting_ [cb-uuid] (have ?cb-fn)) |
|
1372 | 1371 | (or |
1373 | 1372 | (when-let [[s _sid] @socket_] |
1374 | 1373 | (try |
1375 | | - #?(:cljs (.send s ppstr) |
1376 | | - :clj (.send ^WebSocketClient s ^String ppstr)) |
| 1374 | + #?(:cljs (.send s packed) |
| 1375 | + :clj (.send ^WebSocketClient s packed)) |
1377 | 1376 |
|
1378 | 1377 | (reset! udt-last-comms_ (enc/now-udt)) |
1379 | 1378 | :apparent-success |
|
1435 | 1434 | {:udt (enc/now-udt), :ex ex}))))) |
1436 | 1435 |
|
1437 | 1436 | 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)] |
| 1437 | + (fn #?(:cljs [ws-ev] :clj [packed]) |
| 1438 | + (let [#?@[:cljs [packed (enc/oget ws-ev "data")]] |
| 1439 | + ;; `clj` may/not satisfy `event?` since we also receive cb replies here |
| 1440 | + [clj ?cb-uuid] (unpack packer packed)] |
1447 | 1441 |
|
1448 | 1442 | (reset! udt-last-comms_ (enc/now-udt)) |
1449 | 1443 |
|
|
1615 | 1609 | {:method :post |
1616 | 1610 | :timeout-ms (or ?timeout-ms (:timeout-ms ajax-opts) |
1617 | 1611 | default-client-side-ajax-timeout-ms) |
1618 | | - :resp-type :text ; We'll do our own pstr decoding |
| 1612 | + :resp-type :text ; TODO Support binary? |
1619 | 1613 | :headers |
1620 | 1614 | (merge |
1621 | 1615 | (:headers ajax-opts) ; 1st (don't clobber impl.) |
1622 | 1616 | {:X-CSRF-Token csrf-token-str}) |
1623 | 1617 |
|
1624 | 1618 | :params |
1625 | | - (let [ppstr (pack packer ev (when ?cb-fn :ajax-cb))] |
| 1619 | + (let [packed (pack packer ev (when ?cb-fn :ajax-cb))] |
1626 | 1620 | (merge params ; 1st (don't clobber impl.): |
1627 | 1621 | {:udt (enc/now-udt) ; Force uncached resp |
1628 | 1622 |
|
|
1635 | 1629 | ;; implementation: |
1636 | 1630 | :client-id client-id |
1637 | 1631 |
|
1638 | | - :ppstr ppstr}))}) |
| 1632 | + :packed packed}))}) |
1639 | 1633 |
|
1640 | 1634 | (fn ajax-cb [{:keys [?error ?content]}] |
1641 | 1635 | (if ?error |
|
1645 | 1639 | (swap-chsk-state! chsk #(chsk-state->closed % :unexpected)) |
1646 | 1640 | (when ?cb-fn (?cb-fn :chsk/error)))) |
1647 | 1641 |
|
1648 | | - (let [content ?content |
1649 | | - resp-ppstr content |
1650 | | - [resp-clj _] (unpack packer resp-ppstr)] |
| 1642 | + (let [packed ?content |
| 1643 | + [resp-clj _] (unpack packer packed)] |
1651 | 1644 | (if ?cb-fn |
1652 | 1645 | (?cb-fn resp-clj) |
1653 | 1646 | (when (not= resp-clj :chsk/dummy-cb-200) |
|
1678 | 1671 | {:method :get ; :timeout-ms timeout-ms |
1679 | 1672 | :timeout-ms (or (:timeout-ms ajax-opts) |
1680 | 1673 | default-client-side-ajax-timeout-ms) |
1681 | | - :resp-type :text ; Prefer to do our own pstr reading |
| 1674 | + :resp-type :text ; TODO Support binary? |
1682 | 1675 | :xhr-cb-fn (fn [xhr] (reset! curr-xhr_ xhr)) |
1683 | 1676 | :params |
1684 | 1677 | (merge |
|
1714 | 1707 | (retry-fn))) |
1715 | 1708 |
|
1716 | 1709 | ;; The Ajax long-poller is used only for events, never cbs: |
1717 | | - (let [content ?content |
1718 | | - ppstr content |
1719 | | - [clj] (unpack packer ppstr) |
| 1710 | + (let [packed ?content |
| 1711 | + [clj] (unpack packer packed) |
1720 | 1712 | handshake? (handshake? clj)] |
1721 | 1713 |
|
1722 | 1714 | (when handshake? |
|
0 commit comments