mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-02 16:15:59 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b04ab20455 | |||
| 758606f627 | |||
| 71f328ef94 | |||
| 9b94063d60 | |||
| 165eecf037 | |||
| 6890da44c9 |
@@ -15,8 +15,7 @@
|
|||||||
package msggateway
|
package msggateway
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"encoding/json"
|
||||||
"encoding/gob"
|
|
||||||
|
|
||||||
"github.com/openimsdk/tools/errs"
|
"github.com/openimsdk/tools/errs"
|
||||||
)
|
)
|
||||||
@@ -33,19 +32,17 @@ func NewGobEncoder() *GobEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *GobEncoder) Encode(data any) ([]byte, error) {
|
func (g *GobEncoder) Encode(data any) ([]byte, error) {
|
||||||
buff := bytes.Buffer{}
|
b, err := json.Marshal(data)
|
||||||
enc := gob.NewEncoder(&buff)
|
if err != nil {
|
||||||
if err := enc.Encode(data); err != nil {
|
return nil, errs.New("Encoder.Encode failed", "action", "encode")
|
||||||
return nil, errs.WrapMsg(err, "GobEncoder.Encode failed", "action", "encode")
|
|
||||||
}
|
}
|
||||||
return buff.Bytes(), nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GobEncoder) Decode(encodeData []byte, decodeData any) error {
|
func (g *GobEncoder) Decode(encodeData []byte, decodeData any) error {
|
||||||
buff := bytes.NewBuffer(encodeData)
|
err := json.Unmarshal(encodeData, decodeData)
|
||||||
dec := gob.NewDecoder(buff)
|
if err != nil {
|
||||||
if err := dec.Decode(decodeData); err != nil {
|
return errs.New("Encoder.Decode failed", "action", "decode")
|
||||||
return errs.WrapMsg(err, "GobEncoder.Decode failed", "action", "decode")
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package msggateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGobEncoder_Encode(t *testing.T) {
|
||||||
|
encoder := NewGobEncoder()
|
||||||
|
|
||||||
|
// 测试用例1: 编码 []byte 数据
|
||||||
|
inputData := []byte("test data")
|
||||||
|
encodedData, err := encoder.Encode(inputData)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error, got %v", err)
|
||||||
|
}
|
||||||
|
if string(encodedData) != string(inputData) {
|
||||||
|
t.Fatalf("expected encoded data to be '%s', got '%s'", inputData, encodedData)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试用例2: 编码非 []byte 数据
|
||||||
|
nonByteData := "string data"
|
||||||
|
_, err = encoder.Encode(nonByteData)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected an error when encoding non-byte data, got none")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGobEncoder_Decode(t *testing.T) {
|
||||||
|
encoder := NewGobEncoder()
|
||||||
|
|
||||||
|
// 测试用例1: 解码到 []byte 数据
|
||||||
|
encodedData := []byte("test data")
|
||||||
|
var decodedData []byte
|
||||||
|
err := encoder.Decode(encodedData, &decodedData)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected no error, got %v", err)
|
||||||
|
}
|
||||||
|
if string(decodedData) != string(encodedData) {
|
||||||
|
t.Fatalf("expected decoded data to be '%s', got '%s'", encodedData, decodedData)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 测试用例2: 解码到非 []byte 数据
|
||||||
|
var nonByteData string
|
||||||
|
err = encoder.Decode(encodedData, &nonByteData)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected an error when decoding to non-byte data, got none")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -155,6 +155,7 @@ func (s *Server) pushToUser(ctx context.Context, userID string, msgData *sdkws.M
|
|||||||
(client.IsBackground && client.PlatformID != constant.IOSPlatformID) {
|
(client.IsBackground && client.PlatformID != constant.IOSPlatformID) {
|
||||||
err := client.PushMessage(ctx, msgData)
|
err := client.PushMessage(ctx, msgData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.ZWarn(ctx, "online push msg failed", err, "userID", userID, "platformID", client.PlatformID)
|
||||||
userPlatform.ResultCode = int64(servererrs.ErrPushMsgErr.Code())
|
userPlatform.ResultCode = int64(servererrs.ErrPushMsgErr.Code())
|
||||||
} else {
|
} else {
|
||||||
if _, ok := s.pushTerminal[client.PlatformID]; ok {
|
if _, ok := s.pushTerminal[client.PlatformID]; ok {
|
||||||
|
|||||||
Reference in New Issue
Block a user