comparison chat/room.clj @ 2:b4de894a1e2e

initial import
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Oct 2011 00:03:05 -0700
parents
children
comparison
equal deleted inserted replaced
1:8d8278e09888 2:b4de894a1e2e
1 (ns chat.room
2 (:use [clojure.contrib server-socket duck-streams str-utils])
3 (:gen-class))
4
5
6 ;; CHAT ROOMS
7
8 (def rooms (ref {}))
9
10 (defn get-room! "Returns the chat room with the given name, creating it if necessary." [name]
11 (dosync
12 (or (@rooms name)
13 (let [new-room (agent '())]
14 (do (alter *rooms* assoc name new-room))))))
15
16 (defn say-in-room[room message]
17 (doseq [[_ output] room]
18 (binding [*out* output]
19 (println message))))
20
21
22 ;; USERS
23
24 (defn user-join[room username output-channel]
25 (conj room [username output-channel]))
26 (defn user-leave[room username]
27 (remove #(= (% 0) username) room))
28
29
30 (def *username* "Someone")
31 (defn- join-room [room]
32 (send room user-join *username* *out*)
33 (send room
34 )