-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.go
More file actions
50 lines (40 loc) · 796 Bytes
/
manager.go
File metadata and controls
50 lines (40 loc) · 796 Bytes
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
package websockets
import (
cmap "github.com/orcaman/concurrent-map"
)
type Manager struct {
Clients cmap.ConcurrentMap
}
func InitManager() *Manager {
return &Manager{
Clients: cmap.New(),
}
}
// Get 获取客户端
func (m *Manager) Get(id string) *Client {
if client, ok := m.Clients.Get(id); ok {
return client.(*Client)
}
return nil
}
// Join 加入
func (m *Manager) Join(c *Client) bool {
m.Clients.Set(c.Id, c)
return true
}
// Leave 离开
func (m *Manager) Leave(c *Client) bool {
m.Clients.Remove(c.Id)
return true
}
// Broadcast 广播
func (m *Manager) Broadcast(msg SubscriberBody) {
go func() {
for v := range m.Clients.IterBuffered() {
client := v.Val.(*Client)
if client.IsServer {
go client.Server.Protocol.Send(client, msg)
}
}
}()
}