🎉 Add more reliable presence mechanism.

This commit is contained in:
Andrey Antukh 2020-05-14 23:41:25 +02:00 committed by Alonso Torres
parent 4b31a147a9
commit 817c22dc3c
8 changed files with 105 additions and 49 deletions

View file

@ -28,6 +28,7 @@
(declare handle-pointer-update)
(declare handle-page-change)
(declare handle-pointer-send)
(declare send-keepalive)
(s/def ::type keyword?)
(s/def ::message
@ -46,8 +47,11 @@
ptk/WatchEvent
(watch [_ state stream]
(let [wsession (get-in state [:ws file-id])
stoper (rx/filter #(= ::finalize %) stream)]
stoper (rx/filter #(= ::finalize %) stream)
interval (* 1000 60)]
(->> (rx/merge
(->> (rx/timer interval interval)
(rx/map #(send-keepalive file-id)))
(->> (ws/-stream wsession)
(rx/filter #(= :message (:type %)))
(rx/map (comp t/decode :payload))
@ -66,6 +70,15 @@
(rx/take-until stoper))))))
(defn send-keepalive
[file-id]
(ptk/reify ::send-keepalive
ptk/EffectEvent
(effect [_ state stream]
(prn "send-keepalive" file-id)
(when-let [ws (get-in state [:ws file-id])]
(ws/-send ws (t/encode {:type :keepalive}))))))
;; --- Finalize Websocket
(defn finalize