1 Star 0 Fork 0

Wsage / go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
feature_online.go 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
sage 提交于 2023-03-18 14:49 . modify
package ws
import "sync"
//IOnline 联机在线管理(已废弃,请使用 IClientsMgr )
type IOnline interface {
AddClient(c *Client)
DeleteClient(c *Client)
Clients() []*Client //return all clients
Conns() int64 //return clients num
}
func NewOnlineWorker() *OnlineWorker {
return &OnlineWorker{
clients: make(map[*Client]bool, 128),
}
}
//OnlineWorker 联机管理worker
type OnlineWorker struct {
clients map[*Client]bool
//总连接数量
conn int64
mu sync.RWMutex
}
func (w *OnlineWorker) AddClient(c *Client) {
w.mu.Lock()
w.clients[c] = true
w.conn += int64(1)
w.mu.Unlock()
}
func (w *OnlineWorker) DeleteClient(c *Client) {
w.mu.RLock()
_, exist := w.clients[c]
w.mu.RUnlock()
if !exist {
return
}
w.mu.Lock()
delete(w.clients, c)
w.conn -= int64(1)
w.mu.Unlock()
}
func (w *OnlineWorker) 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 *OnlineWorker) Conns() int64 {
return w.conn
}
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.45

搜索帮助