Skip to content

Commit e8ed116

Browse files
committed
added feature: showing count of connected users in all clients
1 parent e89f3de commit e8ed116

File tree

7 files changed

+27
-31
lines changed

7 files changed

+27
-31
lines changed

Clojure-Websockets/resources/public/index-test.html

-25
This file was deleted.

Clojure-Websockets/resources/public/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<li><a href="http://matthiasnehlsen.com/blog/2014/07/17/BirdWatch-in-ClojureScript/" target="_blank">Blog</a></li>
4141
<li><a href="https://github.com/matthiasn/BirdWatch" target="_blank">Project on GitHub</a></li>
4242
<li><a href="#" onclick="BirdWatch.legalStuff()">Legal stuff</a></li>
43+
<li><a href="#">Connected: <strong id="users-count"></strong> users</a></li>
4344
</ul>
4445
</li>
4546
</ul>

Clojure-Websockets/src/clj/birdwatch/main.clj

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@
9999
(when (contains? matches (get @a/subscriptions uid))
100100
(chsk-send! uid [:tweet/new t]))))))
101101

102+
;; loop sending stats about server to all connected clients
103+
(go
104+
(while true
105+
(<! (timeout 2000))
106+
(let [uids (:any @connected-uids)]
107+
(doseq [uid uids]
108+
(chsk-send! uid [:stats/users-count (count uids)])))))
109+
102110
(defn -main
103111
[& args]
104112
(tc/start-twitter-conn!)

Clojure-Websockets/src/cljs/birdwatch/core.cljs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
;;; Om components for the application are initialized here. Their implementation lives in the ui namespace.
2626
(om/root ui/tweets-view state/app {:target (. js/document (getElementById "tweet-frame"))})
2727
(om/root ui/count-view state/app {:target (. js/document (getElementById "tweet-count"))})
28+
(om/root ui/users-count-view state/app {:target (. js/document (getElementById "users-count"))})
2829
(om/root ui/search-view state/app {:target (. js/document (getElementById "search"))})
2930
(om/root ui/sort-buttons-view state/app {:target (. js/document (getElementById "sort-buttons"))})
3031
(om/root ui/pagination-view state/app {:target (. js/document (getElementById "pagination"))})

Clojure-Websockets/src/cljs/birdwatch/tweets.cljs

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@
109109
(defn- event-handler [[id data :as ev] _]
110110
(match [id data]
111111

112-
[:some/tweetchunk chunk]
113-
(put! prev-chunks-chan chunk)
114-
115112
[:chsk/state {:first-open? true}]
116113
(do
117114
(print "Channel socket successfully established!")
@@ -130,7 +127,10 @@
130127
[:tweet/prev-chunk chunk]
131128
(do
132129
(put! prev-chunks-chan chunk)
133-
(load-prev))))
130+
(load-prev))
131+
132+
[:stats/users-count uc]
133+
(swap! state/app assoc :users-count uc) ))
134134

135135
:else (print "Unmatched event: %s" ev)))
136136

Clojure-Websockets/src/cljs/birdwatch/ui.cljs

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
om/IRender
1717
(render [this] (dom/span nil (:count app)))))
1818

19+
(defn users-count-view [app owner]
20+
"rendering users counter"
21+
(reify
22+
om/IRender
23+
(render [this] (dom/span nil (:users-count app)))))
24+
1925
(def find-tweets {:by-id (util/tweets-by-order :tweets-map :by-id)
2026
:by-followers (util/tweets-by-order :tweets-map :by-followers)
2127
:by-retweets (util/tweets-by-order :retweets :by-retweets)

Clojure-Websockets/src/cljs/birdwatch/util.cljs

+7-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,14 @@
7171

7272
(defn initial-state []
7373
"function returning fresh application state"
74-
{:count 0 :n 10 :retweets {}
75-
:tweets-map {} :search-text "" :page 1
74+
{:count 0
75+
:n 10
76+
:retweets {}
77+
:tweets-map {}
78+
:search-text ""
79+
:page 1
7680
:search "*"
81+
:users-count 0
7782
:sorted :by-rt-since-startup
7883
:by-followers (priority-map-by >)
7984
:by-retweets (priority-map-by >)

0 commit comments

Comments
 (0)