|
1122 | 1122 | nil))))))))
|
1123 | 1123 |
|
1124 | 1124 | #?(:clj
|
1125 |
| - (defn- create-java-client-websocket! |
1126 |
| - [{:as opts :keys [onerror-fn onmessage-fn onclose-fn uri-str headers]}] |
| 1125 | + (defn- make-client-ws-java |
| 1126 | + [{:as opts :keys [uri-str headers on-error on-message on-close]}] |
1127 | 1127 | (let [uri (java.net.URI. uri-str)
|
1128 | 1128 |
|
1129 | 1129 | ;; headers
|
|
1136 | 1136 | ws-client
|
1137 | 1137 | (proxy [WebSocketClient] [^java.net.URI uri ^java.util.Map headers]
|
1138 | 1138 | (onOpen [^org.java_websocket.handshake.ServerHandshake handshakedata] nil)
|
1139 |
| - (onError [ex] (onerror-fn ex)) |
1140 |
| - (onMessage [^String message] (onmessage-fn message)) |
1141 |
| - (onClose [code reason remote] (onclose-fn code reason remote)))] |
| 1139 | + (onError [ex] (on-error ex)) |
| 1140 | + (onMessage [^String message] (on-message message)) |
| 1141 | + (onClose [code reason remote] (on-close code reason remote)))] |
1142 | 1142 |
|
1143 | 1143 | ;; JS client attempts to connect right away at construction time.
|
1144 | 1144 | ;; Java client doesn't need to, but we'll do anyway for consistency.
|
1145 | 1145 | (.connect ws-client)
|
1146 | 1146 | (do ws-client))))
|
1147 | 1147 |
|
1148 | 1148 | #?(:cljs
|
1149 |
| - (defn- create-js-client-websocket! |
1150 |
| - [{:as opts |
1151 |
| - :keys [onerror-fn onmessage-fn onclose-fn uri-str headers binary-type]}] |
1152 |
| - |
| 1149 | + (defn- make-client-ws-js |
| 1150 | + [{:as opts :keys [uri-str headers on-error on-message on-close binary-type]}] |
1153 | 1151 | (when-let [WebSocket
|
1154 | 1152 | (or
|
1155 | 1153 | (enc/oget goog/global "WebSocket")
|
|
1158 | 1156 |
|
1159 | 1157 | (let [socket (WebSocket. uri-str)]
|
1160 | 1158 | (doto socket
|
1161 |
| - (aset "onerror" onerror-fn) |
1162 |
| - (aset "onmessage" onmessage-fn) ; Nb receives both push & cb evs! |
| 1159 | + (aset "onerror" on-error) |
| 1160 | + (aset "onmessage" on-message) ; Nb receives both push & cb evs! |
1163 | 1161 | ;; Fires repeatedly (on each connection attempt) while server is down:
|
1164 |
| - (aset "onclose" onclose-fn)) |
| 1162 | + (aset "onclose" on-close)) |
1165 | 1163 |
|
1166 | 1164 | (when-let [bt binary-type] ; "arraybuffer" or "blob" (js default)
|
1167 | 1165 | (aset socket "binaryType" bt))
|
1168 | 1166 |
|
1169 | 1167 | socket))))
|
1170 | 1168 |
|
1171 |
| -(defn- create-websocket! [{:as opts :keys [onerror-fn onmessage-fn onclose-fn uri-str headers]}] |
1172 |
| - #?(:cljs (create-js-client-websocket! opts) |
1173 |
| - :clj (create-java-client-websocket! opts))) |
| 1169 | +(defn- default-client-ws-constructor |
| 1170 | + [{:as opts :keys [on-error on-message on-close uri-str headers]}] |
| 1171 | + #?(:cljs (make-client-ws-js opts) |
| 1172 | + :clj (make-client-ws-java opts))) |
1174 | 1173 |
|
1175 | 1174 | (defn- get-client-csrf-token-str
|
1176 | 1175 | "Returns non-blank client CSRF token ?string from given token string
|
|
1197 | 1196 | backoff-ms-fn ; (fn [nattempt]) -> msecs
|
1198 | 1197 | cbs-waiting_ ; {<cb-uuid> <fn> ...}
|
1199 | 1198 | socket_
|
1200 |
| - udt-last-comms_] |
| 1199 | + udt-last-comms_ |
| 1200 | + ws-constructor] |
1201 | 1201 |
|
1202 | 1202 | IChSocket
|
1203 | 1203 | (-chsk-disconnect! [chsk reason]
|
|
1266 | 1266 | (swap-chsk-state! chsk
|
1267 | 1267 | #(assoc % :udt-next-reconnect udt-next-reconnect)))))
|
1268 | 1268 |
|
1269 |
| - onerror-fn |
| 1269 | + on-error |
1270 | 1270 | #?(:cljs
|
1271 | 1271 | (fn [ws-ev]
|
1272 | 1272 | (errorf ; ^:meta {:raw-console? true}
|
|
1286 | 1286 | #(assoc % :last-ws-error
|
1287 | 1287 | {:udt (enc/now-udt), :ex ex}))))
|
1288 | 1288 |
|
1289 |
| - onmessage-fn ; Nb receives both push & cb evs! |
| 1289 | + on-message ; Nb receives both push & cb evs! |
1290 | 1290 | (fn #?(:cljs [ws-ev] :clj [ppstr])
|
1291 | 1291 | (let [ppstr #?(:clj ppstr
|
1292 | 1292 | :cljs (enc/oget ws-ev "data"))
|
|
1319 | 1319 |
|
1320 | 1320 | ;; Fires repeatedly (on each connection attempt) while
|
1321 | 1321 | ;; server is down:
|
1322 |
| - onclose-fn |
| 1322 | + on-close |
1323 | 1323 | (fn #?(:cljs [ws-ev] :clj [code reason remote])
|
1324 | 1324 | (let [last-ws-close
|
1325 | 1325 | #?(:clj
|
|
1350 | 1350 |
|
1351 | 1351 | ?socket
|
1352 | 1352 | (try
|
1353 |
| - (create-websocket! |
| 1353 | + (ws-constructor |
1354 | 1354 | (merge ws-opts
|
1355 |
| - {:onerror-fn onerror-fn |
1356 |
| - :onmessage-fn onmessage-fn |
1357 |
| - :onclose-fn onclose-fn |
1358 |
| - :headers headers |
| 1355 | + {:on-error on-error |
| 1356 | + :on-message on-message |
| 1357 | + :on-close on-close |
| 1358 | + :headers headers |
1359 | 1359 | :uri-str
|
1360 | 1360 | (enc/merge-url-with-query-string url
|
1361 | 1361 | (merge params ; 1st (don't clobber impl.):
|
|
1704 | 1704 | ; w/in given msecs. Should be different to server's :ws-kalive-ms.
|
1705 | 1705 | :ws-kalive-ping-timeout-ms ; When above keep-alive ping is triggered, use this
|
1706 | 1706 | ; timeout (default: 5000) before regarding the connection
|
1707 |
| - ; as broken." |
| 1707 | + ; as broken. |
| 1708 | +
|
| 1709 | + :ws-constructor ; Advanced, (fn [{:keys [uri-str headers on-message on-error on-close]}] |
| 1710 | + ; => connected WebSocket, see `default-client-ws-constructor` code for |
| 1711 | + ; details." |
1708 | 1712 |
|
1709 | 1713 | [path ?csrf-token-or-fn &
|
1710 |
| - [{:keys [type protocol host port params headers recv-buf-or-n packer |
1711 |
| - ws-kalive-ms ws-kalive-ping-timeout-ms ws-opts |
| 1714 | + [{:as opts |
| 1715 | + :keys [type protocol host port params headers recv-buf-or-n packer |
| 1716 | + ws-constructor ws-kalive-ms ws-kalive-ping-timeout-ms ws-opts |
1712 | 1717 | client-id ajax-opts wrap-recv-evs? backoff-ms-fn]
|
1713 | 1718 |
|
1714 |
| - :as opts |
1715 | 1719 | :or {type :auto
|
1716 | 1720 | recv-buf-or-n (async/sliding-buffer 2048) ; Mostly for buffered-evs
|
1717 | 1721 | packer :edn
|
|
1721 | 1725 | backoff-ms-fn enc/exp-backoff
|
1722 | 1726 |
|
1723 | 1727 | ws-kalive-ms 20000
|
1724 |
| - ws-kalive-ping-timeout-ms 5000}} |
| 1728 | + ws-kalive-ping-timeout-ms 5000 |
| 1729 | + ws-constructor default-client-ws-constructor}} |
1725 | 1730 |
|
1726 | 1731 | _deprecated-more-opts]]
|
1727 | 1732 |
|
|
1774 | 1779 | :headers headers
|
1775 | 1780 | :packer packer
|
1776 | 1781 | :ws-kalive-ms ws-kalive-ms
|
1777 |
| - :ws-kalive-ping-timeout-ms ws-kalive-ping-timeout-ms} |
| 1782 | + :ws-kalive-ping-timeout-ms ws-kalive-ping-timeout-ms |
| 1783 | + :ws-constructor default-client-ws-constructor} |
1778 | 1784 |
|
1779 | 1785 | ws-chsk-opts
|
1780 | 1786 | (merge common-chsk-opts
|
|
0 commit comments