/
Pool.go
75 lines (68 loc) · 1.7 KB
/
Pool.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package websocket
import (
"encoding/json"
"fmt"
)
type Pool struct {
Rooms map[string]*Room
}
func InitPools() *Pool {
return &Pool{
Rooms: make(map[string]*Room),
}
}
type Room struct {
ID string
Register chan *Client
Unregister chan *Client
Clients map[*Client]bool
Broadcast chan Message
}
func NewRoom(id string) *Room {
return &Room{
ID: id,
Register: make(chan *Client),
Unregister: make(chan *Client),
Clients: make(map[*Client]bool),
Broadcast: make(chan Message),
}
}
func (room *Room) Start(pool *Pool) {
defer func() {
fmt.Println("Closing Room")
delete(pool.Rooms, room.ID)
}()
for {
select {
case client := <-room.Register:
room.Clients[client] = true
fmt.Println("Size of Connection Room: ", len(room.Clients))
for c := range room.Clients {
user, _ := json.Marshal(&Client{ID: c.ID, Name: c.Name})
c.Conn.WriteJSON(Message{Type: 2, Body: fmt.Sprint(client.Name, " has Joined..."), User: string(user), Room: room.ID})
}
break
case client := <-room.Unregister:
delete(room.Clients, client)
fmt.Println("Size of Connection Room: ", len(room.Clients))
if len(room.Clients) == 0 {
fmt.Println("No one left in room")
return
} else {
for c := range room.Clients {
user, _ := json.Marshal(&Client{ID: c.ID, Name: c.Name})
c.Conn.WriteJSON(Message{Type: 3, Body: fmt.Sprint(client.Name, " has Disconnected..."), User: string(user), Room: room.ID})
}
}
break
case message := <-room.Broadcast:
fmt.Println("Sending message to all clients in room")
for c := range room.Clients {
if err := c.Conn.WriteJSON(message); err != nil {
fmt.Println(err)
return
}
}
}
}
}