fix: ws 消息格式统一

This commit is contained in:
TsMask
2024-01-24 14:10:23 +08:00
parent 0210d28cdb
commit 3135ff5b85
2 changed files with 22 additions and 12 deletions

View File

@@ -82,17 +82,23 @@ func (s *WSController) Test(c *gin.Context) {
return
}
// err = s.wsSendService.ByClientID(c.Query("clientId"), loginUser)
// if err != nil {
// c.JSON(200, result.ErrMsg(err.Error()))
// return
// }
errMsgArr := []string{}
err = s.wsSendService.ByGroupID(c.Query("groupID"), loginUser)
clientId := c.Query("clientId")
if clientId != "" {
err := s.wsSendService.ByClientID(c.Query("clientId"), loginUser)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
errMsgArr = append(errMsgArr, "clientId: "+err.Error())
}
}
c.JSON(200, result.Ok(nil))
groupID := c.Query("groupID")
if groupID != "" {
err := s.wsSendService.ByGroupID(c.Query("groupID"), loginUser)
if err != nil {
errMsgArr = append(errMsgArr, "groupID: "+err.Error())
}
}
c.JSON(200, result.OkData(errMsgArr))
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"ems.agt/src/framework/vo/result"
"ems.agt/src/modules/ws/model"
)
@@ -29,7 +30,7 @@ func (s *WSSendImpl) ByClientID(clientID string, data any) error {
return fmt.Errorf("no fount client ID: %s", clientID)
}
dataByte, err := json.Marshal(data)
dataByte, err := json.Marshal(result.OkData(data))
if err != nil {
return err
}
@@ -61,7 +62,10 @@ func (s *WSSendImpl) ByGroupID(groupID string, data any) error {
// 在用户中找到客户端并发送
uidClientIds := clientIds.(*[]string)
for _, clientId := range *uidClientIds {
err := s.ByClientID(clientId, data)
err := s.ByClientID(clientId, map[string]any{
"groupID": groupID,
"data": data,
})
if err != nil {
continue
}