This commit is contained in:
Gordon 2021-10-22 20:02:29 +08:00
parent 2e0cb09cdd
commit d88ba3545b
9 changed files with 31 additions and 16 deletions

View File

@ -7,23 +7,27 @@ import (
"Open_IM/src/grpc-etcdv3/getcdv3" "Open_IM/src/grpc-etcdv3/getcdv3"
pbChat "Open_IM/src/proto/chat" pbChat "Open_IM/src/proto/chat"
"Open_IM/src/utils" "Open_IM/src/utils"
"bytes"
"context" "context"
"encoding/json" "encoding/gob"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"runtime" "runtime"
"strings" "strings"
) )
func (ws *WServer) msgParse(conn *UserConn, jsonMsg []byte) { func (ws *WServer) msgParse(conn *UserConn, binaryMsg []byte) {
//ws online debug data //ws online debug data
//{"ReqIdentifier":1001,"Token":"123","SendID":"c4ca4238a0b923820dcc509a6f75849b","Time":"123","OperationID":"123","MsgIncr":0} //{"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":1002,"Token":"123","SendID":"c4ca4238a0b923820dcc509a6f75849b","Time":"123","OperationID":"123","MsgIncr":0,"SeqBegin":1,"SeqEnd":6}
//{"ReqIdentifier":1003,"Token":"123","SendID":"c4ca4238a0b923820dcc509a6f75849b", //{"ReqIdentifier":1003,"Token":"123","SendID":"c4ca4238a0b923820dcc509a6f75849b",
//"RecvID":"a87ff679a2f3e71d9181a67b7542122c","ClientMsgID":"2343","Time":"147878787","OperationID": //"RecvID":"a87ff679a2f3e71d9181a67b7542122c","ClientMsgID":"2343","Time":"147878787","OperationID":
//"123","MsgIncr":0,"SubMsgType":101,"MsgType":100,"MsgFrom":1,"Content":"sdfsdf"} //"123","MsgIncr":0,"SubMsgType":101,"MsgType":100,"MsgFrom":1,"Content":"sdfsdf"}
b := bytes.NewBuffer(binaryMsg)
m := Req{} 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()) log.ErrorByKv("ws json Unmarshal err", "", "err", err.Error())
ws.sendErrMsg(conn, 200, err.Error(), constant.WSDataError, "") ws.sendErrMsg(conn, 200, err.Error(), constant.WSDataError, "")
err = conn.Close() 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{}) { func (ws *WServer) sendMsg(conn *UserConn, mReply interface{}) {
bMsg, _ := json.Marshal(mReply) var b bytes.Buffer
err := ws.writeMsg(conn, websocket.TextMessage, bMsg) 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 { if err != nil {
log.ErrorByKv("WS WriteMsg error", "", "userIP", conn.RemoteAddr().String(), "userUid", ws.getUserUid(conn), "error", err, "mReply", mReply) log.ErrorByKv("WS WriteMsg error", "", "userIP", conn.RemoteAddr().String(), "userUid", ws.getUserUid(conn), "error", err, "mReply", mReply)
} }

View File

@ -7,8 +7,9 @@ import (
"Open_IM/src/grpc-etcdv3/getcdv3" "Open_IM/src/grpc-etcdv3/getcdv3"
pbRelay "Open_IM/src/proto/relay" pbRelay "Open_IM/src/proto/relay"
"Open_IM/src/utils" "Open_IM/src/utils"
"bytes"
"context" "context"
"encoding/json" "encoding/gob"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -74,7 +75,12 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR
msg["sendTime"] = in.SendTime msg["sendTime"] = in.SendTime
msg["senderPlatformID"] = in.PlatformID msg["senderPlatformID"] = in.PlatformID
mReply["data"] = msg 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() { switch in.GetSessionType() {
case constant.SingleChatType: case constant.SingleChatType:
RecvID = in.GetRecvID() RecvID = in.GetRecvID()
@ -87,7 +93,7 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR
if conn := ws.getUserConn(v); conn != nil { if conn := ws.getUserConn(v); conn != nil {
UIDAndPID := strings.Split(v, " ") UIDAndPID := strings.Split(v, " ")
tag = true tag = true
resultCode := sendMsgToUser(conn, bMsg, in, UIDAndPID[1], UIDAndPID[0]) resultCode := sendMsgToUser(conn, b.Bytes(), in, UIDAndPID[1], UIDAndPID[0])
temp := &pbRelay.SingleMsgToUser{ temp := &pbRelay.SingleMsgToUser{
ResultCode: resultCode, ResultCode: resultCode,
RecvID: UIDAndPID[0], 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) { 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 { if err != nil {
log.ErrorByKv("PushMsgToUser is failed By Ws", "", "Addr", conn.RemoteAddr().String(), 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) "error", err, "senderPlatform", utils.PlatformIDToName(in.PlatformID), "recvPlatform", RecvPlatForm, "args", in.String(), "recvID", RecvID)

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package pbAuth; package pbAuth;
option go_package = "auth;pbAuth"; option go_package = "./auth;pbAuth";
message UserRegisterReq { message UserRegisterReq {
string UID = 1; string UID = 1;

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
package pbChat;//The package name to which the proto file belongs 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{ message WSToMsgSvrChatMsg{
string SendID = 1; string SendID = 1;

View File

@ -1,5 +1,5 @@
syntax = "proto3"; syntax = "proto3";
option go_package = "friend;friend"; option go_package = "./friend;friend";
package friend; package friend;
message CommonResp{ message CommonResp{

View File

@ -1,5 +1,5 @@
syntax = "proto3"; syntax = "proto3";
option go_package = "group;group"; option go_package = "./group;group";
package group; package group;
message CommonResp{ message CommonResp{

View File

@ -1,5 +1,5 @@
syntax = "proto3"; syntax = "proto3";
option go_package = "push;pbPush"; option go_package = "./push;pbPush";
package push; package push;
message PushMsgReq { message PushMsgReq {

View File

@ -1,5 +1,5 @@
syntax = "proto3"; syntax = "proto3";
option go_package = "relay;pbRelay"; option go_package = "./relay;pbRelay";
package relay; package relay;
message MsgToUserReq { message MsgToUserReq {

View File

@ -1,5 +1,5 @@
syntax = "proto3"; syntax = "proto3";
option go_package = "user;user"; option go_package = "./user;user";
package user; package user;
message CommonResp{ message CommonResp{