-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.go
52 lines (43 loc) · 1.08 KB
/
room.go
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
42
43
44
45
46
47
48
49
50
51
52
package main
type Room struct {
Name string `json:"name"`
clients map[*Client]bool
register chan *Client
unregister chan *Client
broadcast chan *Message
}
// NewRoom creates a new Room
func NewRoom(name string) *Room {
return &Room{
Name: name,
clients: make(map[*Client]bool),
register: make(chan *Client),
unregister: make(chan *Client),
broadcast: make(chan *Message),
}
}
// RunRoom runs our room, accepting various requests
func (room *Room) RunRoom() {
for {
select {
case client := <-room.register:
room.registerClientInRoom(client)
case client := <-room.unregister:
room.unregisterClientInRoom(client)
case message := <-room.broadcast:
room.broadcastToClientsInRoom(message.encode())
}
}
}
func (room *Room) registerClientInRoom(client *Client) {
// room.notifyClientJoined(client)
room.clients[client] = true
}
func (room *Room) unregisterClientInRoom(client *Client) {
delete(room.clients, client)
}
func (room *Room) broadcastToClientsInRoom(message []byte) {
for client := range room.clients {
client.send <- message
}
}