-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocketPool.go
45 lines (42 loc) · 1.02 KB
/
socketPool.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
package main
// SocketPool is the struct that keep track of all Client connection
type SocketPool struct {
clients map[*Client]bool
broadcast chan []byte
register chan *Client
unregister chan *Client
}
// NewSocketPool : Factory for the socket pool
func NewSocketPool() *SocketPool {
return &SocketPool{
broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]bool),
}
}
// Just a loop checking actionable on the different channel
// Don't worry, it's running concurently, and it's not blocking
// the main thread
func (s *SocketPool) run() {
for {
select {
case client := <-s.register:
s.clients[client] = true
case client := <-s.unregister:
if _, ok := s.clients[client]; ok {
delete(s.clients, client)
close(client.send)
}
case message := <-s.broadcast:
for client := range s.clients {
select {
case client.send <- message:
default:
close(client.send)
delete(s.clients, client)
}
}
}
}
}