package ws import ( "sync" "github.com/go-admin-team/go-admin-core/logger" ) // Manager 所有 websocket 信息 type Manager struct { Group map[string]map[string]*Client groupCount, clientCount uint Lock sync.Mutex Register, UnRegister chan *Client Message chan *MessageData MessageString chan *MessageDataString GroupMessage chan *GroupMessageData BroadCastMessage chan *BroadCastMessageData } // Start 启动 websocket 管理器 func (manager *Manager) Start() { for { select { // 注册 case client := <-manager.Register: logger.Debugf("client [%s] connect", client.ID) logger.Debugf("register client [%s] to group [%s]", client.ID, client.Group) manager.Lock.Lock() if manager.Group[client.Group] == nil { manager.Group[client.Group] = make(map[string]*Client) manager.groupCount += 1 } manager.Group[client.Group][client.ID] = client manager.clientCount += 1 manager.Lock.Unlock() // 注销 case client := <-manager.UnRegister: logger.Infof("unregister client [%s] from group [%s]", client.ID, client.Group) manager.Lock.Lock() if _, ok := manager.Group[client.Group]; ok { if _, ok := manager.Group[client.Group][client.ID]; ok { close(client.Message) delete(manager.Group[client.Group], client.ID) manager.clientCount -= 1 if len(manager.Group[client.Group]) == 0 { //log.Printf("delete empty group [%s]", client.Group) delete(manager.Group, client.Group) manager.groupCount -= 1 } } } manager.Lock.Unlock() // 发送广播数据到某个组的 channel 变量 Send 中 //case data := <-manager.boardCast: // if groupMap, ok := manager.wsGroup[data.GroupId]; ok { // for _, conn := range groupMap { // conn.Send <- data.Data // } // } } } } // SendService 处理单个 client 发送数据 func (manager *Manager) SendService() { for { select { case data := <-manager.Message: if groupMap, ok := manager.Group[data.Group]; ok { if conn, ok := groupMap[data.ID]; ok { conn.Message <- data.Message } } } } } // SendGroupService 处理 group 广播数据 func (manager *Manager) SendGroupService() { for { select { // 发送广播数据到某个组的 channel 变量 Send 中 case data := <-manager.GroupMessage: if groupMap, ok := manager.Group[data.Group]; ok { for _, conn := range groupMap { conn.Message <- data.Message } } } } } // SendAllService 处理广播数据 func (manager *Manager) SendAllService() { for { select { case data := <-manager.BroadCastMessage: for _, v := range manager.Group { for _, conn := range v { conn.Message <- data.Message } } } } } // Send 向指定的 client 发送数据 func (manager *Manager) Send(id string, group string, message []byte) { data := &MessageData{ ID: id, Group: group, Message: message, } manager.Message <- data } // SendString 向指定的 client 发送数据 func (manager *Manager) SendString(id string, group string, message string) { data := &MessageDataString{ ID: id, Group: group, Message: message, } manager.MessageString <- data } // SendGroup 向指定的 Group 广播 func (manager *Manager) SendGroup(group string, message []byte) { data := &GroupMessageData{ Group: group, Message: message, } manager.GroupMessage <- data } // SendAll 广播 func (manager *Manager) SendAll(message []byte) { data := &BroadCastMessageData{ Message: message, } manager.BroadCastMessage <- data } // RegisterClient 注册 func (manager *Manager) RegisterClient(client *Client) { manager.Register <- client } // UnRegisterClient 注销 func (manager *Manager) UnRegisterClient(client *Client) { manager.UnRegister <- client } // LenGroup 当前组个数 func (manager *Manager) LenGroup() uint { return manager.groupCount } // LenClient 当前连接个数 func (manager *Manager) LenClient() uint { return manager.clientCount } // Info 获取 wsManager 管理器信息 func (manager *Manager) Info() map[string]interface{} { managerInfo := make(map[string]interface{}) managerInfo["groupLen"] = manager.LenGroup() managerInfo["clientLen"] = manager.LenClient() managerInfo["chanRegisterLen"] = len(manager.Register) managerInfo["chanUnregisterLen"] = len(manager.UnRegister) managerInfo["chanMessageLen"] = len(manager.Message) managerInfo["chanGroupMessageLen"] = len(manager.GroupMessage) managerInfo["chanBroadCastMessageLen"] = len(manager.BroadCastMessage) return managerInfo }