This commit is contained in:
苏尹岚
2020-12-03 18:25:02 +08:00
parent 72c4b6c06a
commit 1097cbb60e
2 changed files with 28 additions and 15 deletions

View File

@@ -67,7 +67,7 @@ type ImMessageRecord struct {
Weight int `json:"weight"`
Height int `json:"height"`
// UnReadFlag int `json:"unReadFlag"` //未读标志为1则此条消息未读
Key string `orm:"-" json:"key"`
UserInfo *GetUserResult `orm:"-" json:"userInfo"`
}

View File

@@ -32,6 +32,7 @@ type EventController struct {
//连接的客户端,吧每个客户端都放进来
var clients = make(map[int]map[string]*websocket.Conn)
var clientsHeart = make(map[string]*websocket.Conn)
//广播频道(通道)
var broadcast = make(chan *model.ImMessageRecord)
@@ -74,6 +75,7 @@ func (c *EventController) TestWebsocket() {
return
}
clientUser[userID] = ws
clientsHeart[userID] = ws
for _, v := range messageGroups {
if len(clients[v.GroupID]) > 0 {
clients[v.GroupID][userID] = ws
@@ -147,21 +149,32 @@ func handleMessages() {
for {
//读取通道中的消息
msg := <-broadcast
globals.SugarLogger.Debugf("clients len %v", len(clients))
//循环map客户端
for userID, client := range clients[msg.GroupID] {
//把通道中的消息发送给客户端
user, err := dao.GetUser(dao.GetDB(), msg.UserID)
if err == nil {
msg.UserInfo = user
if msg.GroupID == 0 {
globals.SugarLogger.Debugf("heart %v", utils.Format4Output(msg, false))
if err := clientsHeart[msg.UserID].WriteJSON(&model.ImMessageRecord{
Key: "pang",
}); err != nil {
globals.SugarLogger.Debugf("heart client.WriteJSON error: %v", err)
clientsHeart[msg.UserID].Close() //关闭
delete(clientsHeart, msg.UserID)
}
globals.SugarLogger.Debugf("msg %v", utils.Format4Output(msg, false))
err = client.WriteJSON(msg)
if err != nil {
globals.SugarLogger.Debugf("client.WriteJSON error: %v", err)
client.Close() //关闭
delete(clients[msg.GroupID], userID)
// delete(clients, client) //删除map中的客户端
} else {
globals.SugarLogger.Debugf("clients len %v", len(clients))
//循环map客户端
for userID, client := range clients[msg.GroupID] {
//把通道中的消息发送给客户端
user, err := dao.GetUser(dao.GetDB(), msg.UserID)
if err == nil {
msg.UserInfo = user
}
globals.SugarLogger.Debugf("msg %v", utils.Format4Output(msg, false))
err = client.WriteJSON(msg)
if err != nil {
globals.SugarLogger.Debugf("client.WriteJSON error: %v", err)
client.Close() //关闭
delete(clients[msg.GroupID], userID)
// delete(clients, client) //删除map中的客户端
}
}
}
}