Skip to content

Commit

Permalink
Add a basic broadcast for all websocket connections
Browse files Browse the repository at this point in the history
- Exclude the active receiving connection from broadcast
  • Loading branch information
kapilreddy committed Oct 26, 2024
1 parent 0e13a8c commit a4faf74
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion r_place_backend/src/r_place_backend/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@

(defonce state (atom []))

(defonce all-channels (atom {}))

(defn on-receive-handler
[channel data]
(let [{:keys [column row color] :as pixel-data}
(read-string data)]
(doseq [[ch-k ch] @all-channels]
(when (not= channel ch)
(if (http-kit/open? ch)
(http-kit/send! ch
(pr-str pixel-data))
(swap! all-channels dissoc ch-k))))
(swap! state conj pixel-data)))

(defn chat-handler [req]
Expand All @@ -35,7 +43,7 @@
(if (http-kit/websocket? channel)
(println "WebSocket channel")
(println "HTTP channel"))

(swap! all-channels assoc (:start-time req) channel)
(doseq [pixel-update @state]
(http-kit/send! channel
(pr-str pixel-update)))
Expand Down

0 comments on commit a4faf74

Please sign in to comment.