view chat/room.clj @ 6:b2f55bcf6853

fixed comments
author Robert McIntyre <rlm@mit.edu>
date Fri, 28 Oct 2011 04:56:15 -0700
parents b4de894a1e2e
children
line wrap: on
line source
1 (ns chat.room
2 (:use [clojure.contrib server-socket duck-streams str-utils])
3 (:gen-class))
6 ;; CHAT ROOMS
8 (def rooms (ref {}))
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))))))
16 (defn say-in-room[room message]
17 (doseq [[_ output] room]
18 (binding [*out* output]
19 (println message))))
22 ;; USERS
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))
30 (def *username* "Someone")
31 (defn- join-room [room]
32 (send room user-join *username* *out*)
33 (send room
34 )