1 Star 0 Fork 0

lichun-123/leaf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ws_handler.go 833 Bytes
一键复制 编辑 原始数据 按行查看 历史
lichun-123 提交于 1年前 . proto update
package network
import (
"gitee.com/lichun-123/leaf/log"
"sync"
)
type WSHandler struct {
conns map[*WSConn]struct{}
mutexConns sync.Mutex
}
func MkHandler() *WSHandler {
return &WSHandler{
conns: make(map[*WSConn]struct{}),
mutexConns: sync.Mutex{},
}
}
func (h *WSHandler) add(conn *WSConn) {
h.mutexConns.Lock()
defer h.mutexConns.Unlock()
h.conns[conn] = struct{}{}
}
func (h *WSHandler) remove(conn *WSConn) {
h.mutexConns.Lock()
defer h.mutexConns.Unlock()
delete(h.conns, conn)
}
func (h *WSHandler) accept(conn *WSConn) {
h.add(conn)
go h.read(conn)
}
func (h *WSHandler) read(conn *WSConn) {
defer func() {
conn.Close()
h.remove(conn)
}()
for {
msg, err := conn.ReadMsg()
if err != nil {
log.Debug("read message: %v", err)
break
}
go conn.agent.HandleMsg(msg)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/lichun-123/leaf.git
git@gitee.com:lichun-123/leaf.git
lichun-123
leaf
leaf
v0.0.10

搜索帮助