diff --git a/src/msg_gateway/gate/logic.go b/src/msg_gateway/gate/logic.go index 2f99fa8e7..d8bc14e6e 100644 --- a/src/msg_gateway/gate/logic.go +++ b/src/msg_gateway/gate/logic.go @@ -7,23 +7,27 @@ import ( "Open_IM/src/grpc-etcdv3/getcdv3" pbChat "Open_IM/src/proto/chat" "Open_IM/src/utils" + "bytes" "context" - "encoding/json" + "encoding/gob" "fmt" "github.com/gorilla/websocket" "runtime" "strings" ) -func (ws *WServer) msgParse(conn *UserConn, jsonMsg []byte) { +func (ws *WServer) msgParse(conn *UserConn, binaryMsg []byte) { //ws online debug data //{"ReqIdentifier":1001,"Token":"123","SendID":"c4ca4238a0b923820dcc509a6f75849b","Time":"123","OperationID":"123","MsgIncr":0} //{"ReqIdentifier":1002,"Token":"123","SendID":"c4ca4238a0b923820dcc509a6f75849b","Time":"123","OperationID":"123","MsgIncr":0,"SeqBegin":1,"SeqEnd":6} //{"ReqIdentifier":1003,"Token":"123","SendID":"c4ca4238a0b923820dcc509a6f75849b", //"RecvID":"a87ff679a2f3e71d9181a67b7542122c","ClientMsgID":"2343","Time":"147878787","OperationID": //"123","MsgIncr":0,"SubMsgType":101,"MsgType":100,"MsgFrom":1,"Content":"sdfsdf"} + b := bytes.NewBuffer(binaryMsg) m := Req{} - if err := json.Unmarshal(jsonMsg, &m); err != nil { + dec := gob.NewDecoder(b) + err := dec.Decode(&m) + if err != nil { log.ErrorByKv("ws json Unmarshal err", "", "err", err.Error()) ws.sendErrMsg(conn, 200, err.Error(), constant.WSDataError, "") err = conn.Close() @@ -238,8 +242,13 @@ func (ws *WServer) sendMsgReq(conn *UserConn, m *Req, sendTime int64) { } func (ws *WServer) sendMsg(conn *UserConn, mReply interface{}) { - bMsg, _ := json.Marshal(mReply) - err := ws.writeMsg(conn, websocket.TextMessage, bMsg) + var b bytes.Buffer + enc := gob.NewEncoder(&b) + err := enc.Encode(mReply) + if err != nil { + fmt.Println(err) + } + err = ws.writeMsg(conn, websocket.BinaryMessage, b.Bytes()) if err != nil { log.ErrorByKv("WS WriteMsg error", "", "userIP", conn.RemoteAddr().String(), "userUid", ws.getUserUid(conn), "error", err, "mReply", mReply) } diff --git a/src/msg_gateway/gate/rpc_server.go b/src/msg_gateway/gate/rpc_server.go index bab04bb96..0fa2c4aae 100644 --- a/src/msg_gateway/gate/rpc_server.go +++ b/src/msg_gateway/gate/rpc_server.go @@ -7,8 +7,9 @@ import ( "Open_IM/src/grpc-etcdv3/getcdv3" pbRelay "Open_IM/src/proto/relay" "Open_IM/src/utils" + "bytes" "context" - "encoding/json" + "encoding/gob" "fmt" "github.com/gorilla/websocket" "google.golang.org/grpc" @@ -74,7 +75,12 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR msg["sendTime"] = in.SendTime msg["senderPlatformID"] = in.PlatformID mReply["data"] = msg - bMsg, _ := json.Marshal(mReply) + var b bytes.Buffer + enc := gob.NewEncoder(&b) + err := enc.Encode(mReply) + if err != nil { + fmt.Println(err) + } switch in.GetSessionType() { case constant.SingleChatType: RecvID = in.GetRecvID() @@ -87,7 +93,7 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR if conn := ws.getUserConn(v); conn != nil { UIDAndPID := strings.Split(v, " ") tag = true - resultCode := sendMsgToUser(conn, bMsg, in, UIDAndPID[1], UIDAndPID[0]) + resultCode := sendMsgToUser(conn, b.Bytes(), in, UIDAndPID[1], UIDAndPID[0]) temp := &pbRelay.SingleMsgToUser{ ResultCode: resultCode, RecvID: UIDAndPID[0], @@ -165,7 +171,7 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR } func sendMsgToUser(conn *UserConn, bMsg []byte, in *pbRelay.MsgToUserReq, RecvPlatForm, RecvID string) (ResultCode int64) { - err := ws.writeMsg(conn, websocket.TextMessage, bMsg) + err := ws.writeMsg(conn, websocket.BinaryMessage, bMsg) if err != nil { log.ErrorByKv("PushMsgToUser is failed By Ws", "", "Addr", conn.RemoteAddr().String(), "error", err, "senderPlatform", utils.PlatformIDToName(in.PlatformID), "recvPlatform", RecvPlatForm, "args", in.String(), "recvID", RecvID) diff --git a/src/proto/auth/auth.proto b/src/proto/auth/auth.proto index 94c664125..21b82ff40 100644 --- a/src/proto/auth/auth.proto +++ b/src/proto/auth/auth.proto @@ -1,6 +1,6 @@ syntax = "proto3"; package pbAuth; -option go_package = "auth;pbAuth"; +option go_package = "./auth;pbAuth"; message UserRegisterReq { string UID = 1; diff --git a/src/proto/chat/chat.proto b/src/proto/chat/chat.proto index 50f7ef652..19cf454ca 100644 --- a/src/proto/chat/chat.proto +++ b/src/proto/chat/chat.proto @@ -1,6 +1,6 @@ syntax = "proto3"; package pbChat;//The package name to which the proto file belongs -option go_package = "chat;pbChat";//The generated go pb file is in the current directory, and the package name is pbChat +option go_package = "./chat;pbChat";//The generated go pb file is in the current directory, and the package name is pbChat message WSToMsgSvrChatMsg{ string SendID = 1; diff --git a/src/proto/friend/friend.proto b/src/proto/friend/friend.proto index 7cbfc8c41..52b50d277 100644 --- a/src/proto/friend/friend.proto +++ b/src/proto/friend/friend.proto @@ -1,5 +1,5 @@ syntax = "proto3"; -option go_package = "friend;friend"; +option go_package = "./friend;friend"; package friend; message CommonResp{ diff --git a/src/proto/group/group.proto b/src/proto/group/group.proto index 3180e7a2f..ff1821ec2 100644 --- a/src/proto/group/group.proto +++ b/src/proto/group/group.proto @@ -1,5 +1,5 @@ syntax = "proto3"; -option go_package = "group;group"; +option go_package = "./group;group"; package group; message CommonResp{ diff --git a/src/proto/push/push.proto b/src/proto/push/push.proto index 7226aa543..60fddfe3b 100644 --- a/src/proto/push/push.proto +++ b/src/proto/push/push.proto @@ -1,5 +1,5 @@ syntax = "proto3"; -option go_package = "push;pbPush"; +option go_package = "./push;pbPush"; package push; message PushMsgReq { diff --git a/src/proto/relay/relay.proto b/src/proto/relay/relay.proto index 138163a80..56b876c67 100644 --- a/src/proto/relay/relay.proto +++ b/src/proto/relay/relay.proto @@ -1,5 +1,5 @@ syntax = "proto3"; -option go_package = "relay;pbRelay"; +option go_package = "./relay;pbRelay"; package relay; message MsgToUserReq { diff --git a/src/proto/user/user.proto b/src/proto/user/user.proto index d95960125..e1d38fec6 100644 --- a/src/proto/user/user.proto +++ b/src/proto/user/user.proto @@ -1,5 +1,5 @@ syntax = "proto3"; -option go_package = "user;user"; +option go_package = "./user;user"; package user; message CommonResp{