From 9121063ebcf1102503a8f438991a0fd3f982c415 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 29 Jul 2022 15:59:49 +0800 Subject: [PATCH 1/9] add test file --- internal/push/fcm/push.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/push/fcm/push.go b/internal/push/fcm/push.go index ac485a708..33ef97332 100644 --- a/internal/push/fcm/push.go +++ b/internal/push/fcm/push.go @@ -66,6 +66,7 @@ func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, Fail := 0 for i := 0; i < pages; i++ { Msg := new(messaging.MulticastMessage) + Msg.Notification = &messaging.Notification{} Msg.Notification.Body = detailContent Msg.Notification.Title = alert ctx := context.Background() From 92cdff30431cbfe4243c3ef72fd2cc66634b5217 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Fri, 29 Jul 2022 16:01:08 +0800 Subject: [PATCH 2/9] Adjust error code --- internal/api/auth/auth.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/api/auth/auth.go b/internal/api/auth/auth.go index 60bf1d8a5..733d9206b 100644 --- a/internal/api/auth/auth.go +++ b/internal/api/auth/auth.go @@ -150,7 +150,7 @@ func ParseToken(c *gin.Context) { if err := c.BindJSON(¶ms); err != nil { errMsg := " BindJSON failed " + err.Error() log.NewError("0", errMsg) - c.JSON(http.StatusBadRequest, gin.H{"errCode": 200, "errMsg": errMsg}) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": errMsg}) return } @@ -161,7 +161,7 @@ func ParseToken(c *gin.Context) { if !ok { errMsg := params.OperationID + " " + "GetUserIDFromTokenExpireTime failed " + errInfo + " token:" + c.Request.Header.Get("token") log.NewError(params.OperationID, errMsg) - c.JSON(http.StatusInternalServerError, gin.H{"errCode": 200, "errMsg": errMsg}) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg}) return } From ee79728f9d8f390b00defe7d79551b447cf9c7ad Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 29 Jul 2022 16:07:05 +0800 Subject: [PATCH 3/9] add test file --- internal/push/fcm/push.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/push/fcm/push.go b/internal/push/fcm/push.go index 33ef97332..80449907b 100644 --- a/internal/push/fcm/push.go +++ b/internal/push/fcm/push.go @@ -13,6 +13,8 @@ import ( "strconv" ) +const SinglePushCountLimit = 400 + type Fcm struct { FcmMsgCli *messaging.Client } @@ -60,21 +62,21 @@ func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, } tokenlen := len(Tokens) // 500组为一个推送,我们用400好了 - limit := 400 - pages := int((tokenlen-1)/limit + 1) + pages := int((tokenlen-1)/SinglePushCountLimit + 1) Success := 0 Fail := 0 + log.Info(operationID, "fmc args", tokenlen, pages) for i := 0; i < pages; i++ { Msg := new(messaging.MulticastMessage) Msg.Notification = &messaging.Notification{} Msg.Notification.Body = detailContent Msg.Notification.Title = alert ctx := context.Background() - max := (i+1)*limit - 1 + max := (i+1)*SinglePushCountLimit - 1 if max >= tokenlen { max = tokenlen - 1 } - Msg.Tokens = Tokens[i*limit : max] + Msg.Tokens = Tokens[i*SinglePushCountLimit : max] //SendMulticast sends the given multicast message to all the FCM registration tokens specified. //The tokens array in MulticastMessage may contain up to 500 tokens. //SendMulticast uses the `SendAll()` function to send the given message to all the target recipients. From cd3778a6209c922cf047283556747683a77e7528 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 29 Jul 2022 16:09:53 +0800 Subject: [PATCH 4/9] add test file --- internal/push/fcm/push.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/push/fcm/push.go b/internal/push/fcm/push.go index 80449907b..a7a928978 100644 --- a/internal/push/fcm/push.go +++ b/internal/push/fcm/push.go @@ -8,6 +8,7 @@ import ( "context" firebase "firebase.google.com/go" "firebase.google.com/go/messaging" + "fmt" "google.golang.org/api/option" "path/filepath" "strconv" @@ -65,7 +66,7 @@ func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, pages := int((tokenlen-1)/SinglePushCountLimit + 1) Success := 0 Fail := 0 - log.Info(operationID, "fmc args", tokenlen, pages) + fmt.Println(operationID, "fcm args", tokenlen, pages) for i := 0; i < pages; i++ { Msg := new(messaging.MulticastMessage) Msg.Notification = &messaging.Notification{} From ba4988bb9b7b36510db72785b486b0154bd1e419 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Fri, 29 Jul 2022 16:14:17 +0800 Subject: [PATCH 5/9] Adjust error code --- internal/msg_gateway/gate/logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/msg_gateway/gate/logic.go b/internal/msg_gateway/gate/logic.go index 0d18e0653..b9d132cd1 100644 --- a/internal/msg_gateway/gate/logic.go +++ b/internal/msg_gateway/gate/logic.go @@ -54,7 +54,7 @@ func (ws *WServer) msgParse(conn *UserConn, binaryMsg []byte) { ws.pullMsgBySeqListReq(conn, &m) case constant.WsLogoutMsg: log.NewInfo(m.OperationID, "conn.Close()", m.SendID, m.MsgIncr, m.ReqIdentifier) - conn.Close() + // conn.Close() default: log.Error(m.OperationID, "ReqIdentifier failed ", m.SendID, m.MsgIncr, m.ReqIdentifier) } From 08119990814bf7dad20e71146f949e4d9ede4cd4 Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Fri, 29 Jul 2022 16:28:50 +0800 Subject: [PATCH 6/9] Adjust error code --- internal/msg_gateway/gate/ws_server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/msg_gateway/gate/ws_server.go b/internal/msg_gateway/gate/ws_server.go index ff68124a4..53942155f 100644 --- a/internal/msg_gateway/gate/ws_server.go +++ b/internal/msg_gateway/gate/ws_server.go @@ -268,7 +268,7 @@ func (ws *WServer) addUserConn(uid string, platformID int, conn *UserConn, token if callbackResp.ErrCode != 0 { log.NewError(operationID, utils.GetSelfFuncName(), "callbackUserOnline resp:", callbackResp) } - go ws.MultiTerminalLoginRemoteChecker(uid, int32(platformID), token, operationID) + //go ws.MultiTerminalLoginRemoteChecker(uid, int32(platformID), token, operationID) ws.MultiTerminalLoginChecker(uid, platformID, conn, token, operationID) if oldConnMap, ok := ws.wsUserToConn[uid]; ok { oldConnMap[platformID] = conn From 34588f9da366046d5cd91bd6f8cf2b0dd0697e3d Mon Sep 17 00:00:00 2001 From: wenxu12345 <44203734@qq.com> Date: Fri, 29 Jul 2022 16:55:54 +0800 Subject: [PATCH 7/9] Adjust error code --- internal/api/auth/auth.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/api/auth/auth.go b/internal/api/auth/auth.go index 733d9206b..60bf1d8a5 100644 --- a/internal/api/auth/auth.go +++ b/internal/api/auth/auth.go @@ -150,7 +150,7 @@ func ParseToken(c *gin.Context) { if err := c.BindJSON(¶ms); err != nil { errMsg := " BindJSON failed " + err.Error() log.NewError("0", errMsg) - c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": errMsg}) + c.JSON(http.StatusBadRequest, gin.H{"errCode": 200, "errMsg": errMsg}) return } @@ -161,7 +161,7 @@ func ParseToken(c *gin.Context) { if !ok { errMsg := params.OperationID + " " + "GetUserIDFromTokenExpireTime failed " + errInfo + " token:" + c.Request.Header.Get("token") log.NewError(params.OperationID, errMsg) - c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg}) + c.JSON(http.StatusInternalServerError, gin.H{"errCode": 200, "errMsg": errMsg}) return } From 1b927747788c46fae53b895c656c720998158b81 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 29 Jul 2022 17:03:26 +0800 Subject: [PATCH 8/9] add test file --- internal/push/fcm/push.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/internal/push/fcm/push.go b/internal/push/fcm/push.go index a7a928978..7c1f55d00 100644 --- a/internal/push/fcm/push.go +++ b/internal/push/fcm/push.go @@ -5,10 +5,10 @@ import ( "Open_IM/pkg/common/config" "Open_IM/pkg/common/db" "Open_IM/pkg/common/log" + "Open_IM/pkg/tools" "context" firebase "firebase.google.com/go" "firebase.google.com/go/messaging" - "fmt" "google.golang.org/api/option" "path/filepath" "strconv" @@ -47,7 +47,6 @@ func newFcmClient() *Fcm { } func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, opts push.PushOpts) (string, error) { - //需要一个客户端的Token // accounts->registrationToken Tokens := make([]string, 0) for _, account := range accounts { @@ -61,23 +60,16 @@ func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, Tokens = append(Tokens, AndroidfcmToken) } } - tokenlen := len(Tokens) - // 500组为一个推送,我们用400好了 - pages := int((tokenlen-1)/SinglePushCountLimit + 1) Success := 0 Fail := 0 - fmt.Println(operationID, "fcm args", tokenlen, pages) - for i := 0; i < pages; i++ { - Msg := new(messaging.MulticastMessage) - Msg.Notification = &messaging.Notification{} - Msg.Notification.Body = detailContent - Msg.Notification.Title = alert - ctx := context.Background() - max := (i+1)*SinglePushCountLimit - 1 - if max >= tokenlen { - max = tokenlen - 1 - } - Msg.Tokens = Tokens[i*SinglePushCountLimit : max] + result := tools.NewSplitter(SinglePushCountLimit, Tokens).GetSplitResult() + Msg := new(messaging.MulticastMessage) + Msg.Notification = &messaging.Notification{} + Msg.Notification.Body = detailContent + Msg.Notification.Title = alert + ctx := context.Background() + for _, v := range result { + Msg.Tokens = v.Item //SendMulticast sends the given multicast message to all the FCM registration tokens specified. //The tokens array in MulticastMessage may contain up to 500 tokens. //SendMulticast uses the `SendAll()` function to send the given message to all the target recipients. @@ -87,7 +79,9 @@ func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, //Partial failures are indicated by a `BatchResponse` return value. response, err := f.FcmMsgCli.SendMulticast(ctx, Msg) if err != nil { - return "", err + Fail = Fail + len(v.Item) + log.Info(operationID, "some token push err", err.Error(), len(v.Item)) + continue } Success = Success + response.SuccessCount Fail = Fail + response.FailureCount From b75353643742d1b74629f4c8244a7daf22cd860e Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Fri, 29 Jul 2022 17:05:39 +0800 Subject: [PATCH 9/9] add test file --- pkg/tools/tools.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 pkg/tools/tools.go diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go new file mode 100644 index 000000000..a3baa5f3e --- /dev/null +++ b/pkg/tools/tools.go @@ -0,0 +1,28 @@ +package tools + +type SplitResult struct { + Item []string +} +type Splitter struct { + splitCount int + data []string +} + +func NewSplitter(splitCount int, data []string) *Splitter { + return &Splitter{splitCount: splitCount, data: data} +} +func (s *Splitter) GetSplitResult() (result []*SplitResult) { + remain := len(s.data) % s.splitCount + integer := len(s.data) / s.splitCount + for i := 0; i < integer; i++ { + r := new(SplitResult) + r.Item = s.data[i*s.splitCount : (i+1)*s.splitCount] + result = append(result, r) + } + if remain > 0 { + r := new(SplitResult) + r.Item = s.data[integer*s.splitCount:] + result = append(result, r) + } + return result +}