summaryrefslogtreecommitdiff
path: root/buddy.go
blob: 1128956306880fa149ed0069219aae4aca75c6f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
	"time"
)

type Buddy struct {
	Nick    string
	Receive chan Message
	room    *Room
}

func NewBuddy(nick string, room *Room) *Buddy {
	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,
	}
}