summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--buddy.go28
-rw-r--r--rooms.go2
-rw-r--r--websock.go9
3 files changed, 35 insertions, 4 deletions
diff --git a/buddy.go b/buddy.go
index 750e424..1128956 100644
--- a/buddy.go
+++ b/buddy.go
@@ -1,5 +1,9 @@
package main
+import (
+ "time"
+)
+
type Buddy struct {
Nick string
Receive chan Message
@@ -7,9 +11,31 @@ type Buddy struct {
}
func NewBuddy(nick string, room *Room) *Buddy {
- // TODO: Implement me!
+ return &Buddy{
+ Nick: nick,
+ Receive: make(chan Message),
+ room: room,
+ }
}
func (b *Buddy) Leave() {
b.room.Leave(b.Nick)
}
+
+func (b *Buddy) Push(msg Message) {
+ go func() {
+ select {
+ case b.Receive <- msg:
+ case <-time.Tick(time.Millisecond * 100):
+ }
+ }()
+}
+
+// Say sends a text as a chat message of this user to the connected room.
+func (b *Buddy) Say(text string) {
+ b.room.Messages <- Message{
+ Type: MsgChat,
+ User: b.Nick,
+ Text: text,
+ }
+}
diff --git a/rooms.go b/rooms.go
index c732bb6..723fc98 100644
--- a/rooms.go
+++ b/rooms.go
@@ -35,7 +35,7 @@ func (r *Room) Leave(nick string) {
func (r *Room) Broadcast() {
for m := range r.Messages {
for _, buddy := range r.Buddies {
- buddy.Receive <- m // TODO: What happens when this locks?
+ buddy.Push(m)
}
}
}
diff --git a/websock.go b/websock.go
index bdb7546..6b69788 100644
--- a/websock.go
+++ b/websock.go
@@ -3,7 +3,6 @@ package main
import (
"code.google.com/p/go.net/websocket"
"github.com/gorilla/mux"
- "log"
"net/http"
)
@@ -54,8 +53,14 @@ func AcceptWebSock(rw http.ResponseWriter, req *http.Request) {
continue
}
- // TODO: Broadcast messsage
+ buddy.Say(s)
}
}()
+
+ for m := range buddy.Receive {
+ if send(m) != nil {
+ return
+ }
+ }
}).ServeHTTP(rw, req)
}