summaryrefslogtreecommitdiff
path: root/buddy.go
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2014-03-25 14:07:54 +0100
committerKevin Chabowski <kevin@kch42.de>2014-03-25 14:07:54 +0100
commit472848fc93accb200ea2dedc46fdaa4e5d71d266 (patch)
treec1237832870f4bc1c63f6cb68a742f11fd242d79 /buddy.go
parentd3ad46faf520291037d6803e8547cdafe8b0aff3 (diff)
downloadsimplechat-472848fc93accb200ea2dedc46fdaa4e5d71d266.tar.gz
simplechat-472848fc93accb200ea2dedc46fdaa4e5d71d266.tar.bz2
simplechat-472848fc93accb200ea2dedc46fdaa4e5d71d266.zip
Implemented message broadcasting stuff for type Buddy
Diffstat (limited to 'buddy.go')
-rw-r--r--buddy.go28
1 files changed, 27 insertions, 1 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,
+ }
+}