Ai
1 Star 0 Fork 0

Souki/go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
feature_clientmgr.go 996 Bytes
一键复制 编辑 原始数据 按行查看 历史
sage 提交于 2023-07-02 19:33 +08:00 . add subsribe
package ws
import "sync"
type IClientsMgr interface {
AddClient(c *Client)
DeleteClient(c *Client)
Clients() []*Client //return all clients
Conns() int64 //return clients num
}
func NewClientsMgr() *ClientsMgr {
return &ClientsMgr{
clients: make(map[*Client]bool, 128),
}
}
//ClientsMgr 联机客户端管理
type ClientsMgr struct {
clients map[*Client]bool
//总连接数量
conn int64
mu sync.RWMutex
}
func (w *ClientsMgr) AddClient(c *Client) {
w.mu.Lock()
if _, exist := w.clients[c]; !exist {
w.clients[c] = true
w.conn += int64(1)
}
w.mu.Unlock()
}
func (w *ClientsMgr) DeleteClient(c *Client) {
w.mu.Lock()
if _, exist := w.clients[c]; exist {
delete(w.clients, c)
w.conn -= int64(1)
}
w.mu.Unlock()
}
func (w *ClientsMgr) Clients() []*Client {
clients := make([]*Client, 0)
w.mu.RLock()
for c := range w.clients {
clients = append(clients, c)
}
w.mu.RUnlock()
return clients
}
func (w *ClientsMgr) Conns() int64 {
return w.conn
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.46

搜索帮助