From 412bf29f57aa2782227ebbad29ecc751bb9f7e01 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Sat, 25 Oct 2014 22:15:32 +0300 Subject: [PATCH] Close up client channels properly on disconnect --- src/clj/grub/core.clj | 9 +++++++-- src/clj/grub/websocket.clj | 8 ++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/clj/grub/core.clj b/src/clj/grub/core.clj index 8cbc156..f731c7b 100644 --- a/src/clj/grub/core.clj +++ b/src/clj/grub/core.clj @@ -62,9 +62,14 @@ (httpkit/with-channel request ws-channel (let [to-client (chan) from-client (chan) - new-states (chan (a/sliding-buffer 1))] + new-states (chan (a/sliding-buffer 1)) + on-close (fn [] + (a/unsub new-states-pub :new-state new-states) + (a/close! new-states) + (a/close! from-client) + (a/close! to-client))] (a/sub new-states-pub :new-state new-states) - (ws/add-connected-client! ws-channel to-client from-client) + (ws/add-connected-client! ws-channel to-client from-client on-close) (sync/make-server-agent to-client from-client new-states states))) (handler request)))) diff --git a/src/clj/grub/websocket.clj b/src/clj/grub/websocket.clj index f04c62f..5287206 100644 --- a/src/clj/grub/websocket.clj +++ b/src/clj/grub/websocket.clj @@ -22,11 +22,15 @@ received (t/read reader)] received)) -(defn add-connected-client! [ws-channel to from] +(defn add-connected-client! [ws-channel to from on-close] (println "Client connected:" (.toString ws-channel)) (a/go-loop [] (if-let [event (