mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-09 11:35:59 +08:00
send msg file modify
This commit is contained in:
@@ -6,7 +6,7 @@ import (
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||||
pbChat "Open_IM/pkg/proto/chat"
|
||||
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||
sdk_ws "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
"bytes"
|
||||
"context"
|
||||
@@ -86,7 +86,7 @@ func (ws *WServer) getSeqReq(conn *UserConn, m *Req) {
|
||||
}
|
||||
}
|
||||
func (ws *WServer) getSeqResp(conn *UserConn, m *Req, pb *pbChat.GetMaxAndMinSeqResp) {
|
||||
var mReplyData open_im_sdk.GetMaxAndMinSeqResp
|
||||
var mReplyData sdk_ws.GetMaxAndMinSeqResp
|
||||
mReplyData.MaxSeq = pb.GetMaxSeq()
|
||||
mReplyData.MinSeq = pb.GetMinSeq()
|
||||
b, _ := proto.Marshal(&mReplyData)
|
||||
@@ -131,7 +131,7 @@ func (ws *WServer) pullMsgReq(conn *UserConn, m *Req) {
|
||||
}
|
||||
func (ws *WServer) pullMsgResp(conn *UserConn, m *Req, pb *pbChat.PullMessageResp) {
|
||||
log.NewInfo(m.OperationID, "pullMsgResp come here ", pb.String())
|
||||
var mReplyData open_im_sdk.PullMessageBySeqListResp
|
||||
var mReplyData sdk_ws.PullMessageBySeqListResp
|
||||
a, err := json.Marshal(pb.SingleUserMsg)
|
||||
if err != nil {
|
||||
log.NewError(m.OperationID, "GetSingleUserMsg,json marshal,err", err.Error())
|
||||
@@ -170,10 +170,10 @@ func (ws *WServer) pullMsgBySeqListReq(conn *UserConn, m *Req) {
|
||||
log.NewInfo(m.OperationID, "Ws call success to pullMsgBySeqListReq start", m.SendID, m.ReqIdentifier, m.MsgIncr)
|
||||
nReply := new(pbChat.PullMessageResp)
|
||||
isPass, errCode, errMsg, data := ws.argsValidate(m, constant.WSPullMsgBySeqList)
|
||||
log.NewInfo(m.OperationID, "Ws call success to pullMsgBySeqListReq middle", m.SendID, m.ReqIdentifier, m.MsgIncr, data.(open_im_sdk.PullMessageBySeqListReq).SeqList)
|
||||
log.NewInfo(m.OperationID, "Ws call success to pullMsgBySeqListReq middle", m.SendID, m.ReqIdentifier, m.MsgIncr, data.(sdk_ws.PullMessageBySeqListReq).SeqList)
|
||||
if isPass {
|
||||
pbData := pbChat.PullMessageBySeqListReq{}
|
||||
pbData.SeqList = data.(open_im_sdk.PullMessageBySeqListReq).SeqList
|
||||
pbData.SeqList = data.(sdk_ws.PullMessageBySeqListReq).SeqList
|
||||
pbData.UserID = m.SendID
|
||||
pbData.OperationID = m.OperationID
|
||||
grpcConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOfflineMessageName)
|
||||
@@ -196,32 +196,20 @@ func (ws *WServer) pullMsgBySeqListReq(conn *UserConn, m *Req) {
|
||||
}
|
||||
func (ws *WServer) sendMsgReq(conn *UserConn, m *Req, sendTime int64) {
|
||||
log.NewInfo(m.OperationID, "Ws call success to sendMsgReq start", m.MsgIncr, m.ReqIdentifier, m.SendID, sendTime)
|
||||
nReply := new(pbChat.UserSendMsgResp)
|
||||
nReply := new(pbChat.SendMsgResp)
|
||||
isPass, errCode, errMsg, pData := ws.argsValidate(m, constant.WSSendMsg)
|
||||
if isPass {
|
||||
data := pData.(open_im_sdk.UserSendMsgReq)
|
||||
pbData := pbChat.UserSendMsgReq{
|
||||
ReqIdentifier: m.ReqIdentifier,
|
||||
Token: m.Token,
|
||||
SendID: m.SendID,
|
||||
OperationID: m.OperationID,
|
||||
PlatformID: data.PlatformID,
|
||||
SessionType: data.SessionType,
|
||||
MsgFrom: data.MsgFrom,
|
||||
ContentType: data.ContentType,
|
||||
RecvID: data.RecvID,
|
||||
ForceList: data.ForceList,
|
||||
SenderNickName: data.SenderNickName,
|
||||
SenderFaceURL: data.SenderFaceURL,
|
||||
Content: data.Content,
|
||||
Options: utils.MapIntToJsonString(data.Options),
|
||||
ClientMsgID: data.ClientMsgID,
|
||||
SendTime: sendTime,
|
||||
data := pData.(sdk_ws.MsgData)
|
||||
data.SendTime = sendTime
|
||||
pbData := pbChat.SendMsgReq{
|
||||
Token: m.Token,
|
||||
OperationID: m.OperationID,
|
||||
MsgData: &data,
|
||||
}
|
||||
log.NewInfo(m.OperationID, "Ws call success to sendMsgReq middle", m.ReqIdentifier, m.SendID, m.MsgIncr, data)
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImOfflineMessageName)
|
||||
client := pbChat.NewChatClient(etcdConn)
|
||||
reply, err := client.UserSendMsg(context.Background(), &pbData)
|
||||
reply, err := client.SendMsg(context.Background(), &pbData)
|
||||
if err != nil {
|
||||
log.NewError(pbData.OperationID, "UserSendMsg err", err.Error())
|
||||
nReply.ErrCode = 200
|
||||
@@ -239,10 +227,10 @@ func (ws *WServer) sendMsgReq(conn *UserConn, m *Req, sendTime int64) {
|
||||
}
|
||||
|
||||
}
|
||||
func (ws *WServer) sendMsgResp(conn *UserConn, m *Req, pb *pbChat.UserSendMsgResp, sendTime int64) {
|
||||
func (ws *WServer) sendMsgResp(conn *UserConn, m *Req, pb *pbChat.SendMsgResp, sendTime int64) {
|
||||
// := make(map[string]interface{})
|
||||
|
||||
var mReplyData open_im_sdk.UserSendMsgResp
|
||||
var mReplyData sdk_ws.UserSendMsgResp
|
||||
mReplyData.ClientMsgID = pb.GetClientMsgID()
|
||||
mReplyData.ServerMsgID = pb.GetServerMsgID()
|
||||
mReplyData.SendTime = sendTime
|
||||
@@ -263,12 +251,14 @@ func (ws *WServer) sendMsg(conn *UserConn, mReply interface{}) {
|
||||
enc := gob.NewEncoder(&b)
|
||||
err := enc.Encode(mReply)
|
||||
if err != nil {
|
||||
log.NewError(mReply.(Resp).OperationID, mReply.(Resp).ReqIdentifier, mReply.(Resp).ErrCode, mReply.(Resp).ErrMsg, "Encode Msg error", conn.RemoteAddr().String(), ws.getUserUid(conn), err.Error())
|
||||
uid, platform := ws.getUserUid(conn)
|
||||
log.NewError(mReply.(Resp).OperationID, mReply.(Resp).ReqIdentifier, mReply.(Resp).ErrCode, mReply.(Resp).ErrMsg, "Encode Msg error", conn.RemoteAddr().String(), uid, platform, err.Error())
|
||||
return
|
||||
}
|
||||
err = ws.writeMsg(conn, websocket.BinaryMessage, b.Bytes())
|
||||
if err != nil {
|
||||
log.NewError(mReply.(Resp).OperationID, mReply.(Resp).ReqIdentifier, mReply.(Resp).ErrCode, mReply.(Resp).ErrMsg, "WS WriteMsg error", conn.RemoteAddr().String(), ws.getUserUid(conn), err.Error())
|
||||
uid, platform := ws.getUserUid(conn)
|
||||
log.NewError(mReply.(Resp).OperationID, mReply.(Resp).ReqIdentifier, mReply.(Resp).ErrCode, mReply.(Resp).ErrMsg, "WS WriteMsg error", conn.RemoteAddr().String(), uid, platform, err.Error())
|
||||
}
|
||||
}
|
||||
func (ws *WServer) sendErrMsg(conn *UserConn, errCode int32, errMsg string, reqIdentifier int32, msgIncr string, operationID string) {
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/grpc-etcdv3/getcdv3"
|
||||
pbRelay "Open_IM/pkg/proto/relay"
|
||||
open_im_sdk "Open_IM/pkg/proto/sdk_ws"
|
||||
"Open_IM/pkg/utils"
|
||||
"bytes"
|
||||
"context"
|
||||
@@ -55,26 +54,10 @@ func (r *RPCServer) run() {
|
||||
return
|
||||
}
|
||||
}
|
||||
func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbRelay.MsgToUserResp, error) {
|
||||
func (r *RPCServer) OnlinePushMsg(_ context.Context, in *pbRelay.OnlinePushMsgReq) (*pbRelay.OnlinePushMsgResp, error) {
|
||||
log.InfoByKv("PushMsgToUser is arriving", in.OperationID, "args", in.String())
|
||||
var resp []*pbRelay.SingleMsgToUser
|
||||
var RecvID string
|
||||
msg := open_im_sdk.MsgData{
|
||||
SendID: in.SendID,
|
||||
RecvID: in.RecvID,
|
||||
MsgFrom: in.MsgFrom,
|
||||
ContentType: in.ContentType,
|
||||
SessionType: in.SessionType,
|
||||
SenderNickName: in.SenderNickName,
|
||||
SenderFaceURL: in.SenderFaceURL,
|
||||
ClientMsgID: in.ClientMsgID,
|
||||
ServerMsgID: in.ServerMsgID,
|
||||
Content: in.Content,
|
||||
Seq: in.RecvSeq,
|
||||
SendTime: in.SendTime,
|
||||
SenderPlatformID: in.PlatformID,
|
||||
}
|
||||
msgBytes, _ := proto.Marshal(&msg)
|
||||
msgBytes, _ := proto.Marshal(in.MsgData)
|
||||
mReply := Resp{
|
||||
ReqIdentifier: constant.WSPushMsg,
|
||||
OperationID: in.OperationID,
|
||||
@@ -86,65 +69,52 @@ func (r *RPCServer) MsgToUser(_ context.Context, in *pbRelay.MsgToUserReq) (*pbR
|
||||
if err != nil {
|
||||
log.NewError(in.OperationID, "data encode err", err.Error())
|
||||
}
|
||||
switch in.GetSessionType() {
|
||||
case constant.SingleChatType:
|
||||
RecvID = in.GetRecvID()
|
||||
case constant.GroupChatType:
|
||||
RecvID = strings.Split(in.GetRecvID(), " ")[0]
|
||||
}
|
||||
var tag bool
|
||||
var UIDAndPID []string
|
||||
userIDList := genUidPlatformArray(RecvID)
|
||||
for _, v := range userIDList {
|
||||
UIDAndPID = strings.Split(v, " ")
|
||||
if conn := ws.getUserConn(v); conn != nil {
|
||||
recvID := in.MsgData.RecvID
|
||||
platformList := genPlatformArray()
|
||||
for _, v := range platformList {
|
||||
if conn := ws.getUserConn(recvID, v); conn != nil {
|
||||
tag = true
|
||||
resultCode := sendMsgToUser(conn, replyBytes.Bytes(), in, UIDAndPID[1], UIDAndPID[0])
|
||||
resultCode := sendMsgToUser(conn, replyBytes.Bytes(), in, v, recvID)
|
||||
temp := &pbRelay.SingleMsgToUser{
|
||||
ResultCode: resultCode,
|
||||
RecvID: UIDAndPID[0],
|
||||
RecvPlatFormID: constant.PlatformNameToID(UIDAndPID[1]),
|
||||
RecvID: recvID,
|
||||
RecvPlatFormID: constant.PlatformNameToID(v),
|
||||
}
|
||||
resp = append(resp, temp)
|
||||
} else {
|
||||
temp := &pbRelay.SingleMsgToUser{
|
||||
ResultCode: -1,
|
||||
RecvID: UIDAndPID[0],
|
||||
RecvPlatFormID: constant.PlatformNameToID(UIDAndPID[1]),
|
||||
RecvID: recvID,
|
||||
RecvPlatFormID: constant.PlatformNameToID(v),
|
||||
}
|
||||
resp = append(resp, temp)
|
||||
}
|
||||
}
|
||||
//Single chat sender synchronization message
|
||||
if in.GetSessionType() == constant.SingleChatType && in.ContentType <= constant.Quote && in.ContentType != constant.Typing && in.ContentType != constant.HasReadReceipt {
|
||||
userIDList = genUidPlatformArray(in.SendID)
|
||||
for _, v := range userIDList {
|
||||
UIDAndPID = strings.Split(v, " ")
|
||||
if conn := ws.getUserConn(v); conn != nil {
|
||||
_ = sendMsgToUser(conn, replyBytes.Bytes(), in, UIDAndPID[1], UIDAndPID[0])
|
||||
}
|
||||
if in.MsgData.GetSessionType() == constant.SingleChatType {
|
||||
for k, v := range ws.getSingleUserAllConn(recvID) {
|
||||
_ = sendMsgToUser(v, replyBytes.Bytes(), in, k, recvID)
|
||||
}
|
||||
}
|
||||
if !tag {
|
||||
log.NewError(in.OperationID, "push err ,no matched ws conn not in map", in.String())
|
||||
}
|
||||
return &pbRelay.MsgToUserResp{
|
||||
return &pbRelay.OnlinePushMsgResp{
|
||||
Resp: resp,
|
||||
}, nil
|
||||
}
|
||||
func (r *RPCServer) GetUsersOnlineStatus(_ context.Context, req *pbRelay.GetUsersOnlineStatusReq) (*pbRelay.GetUsersOnlineStatusResp, error) {
|
||||
log.NewDebug(req.OperationID, "rpc GetUsersOnlineStatus arrived server", req.String())
|
||||
var UIDAndPID []string
|
||||
var resp pbRelay.GetUsersOnlineStatusResp
|
||||
for _, v1 := range req.UserIDList {
|
||||
userIDList := genUidPlatformArray(v1)
|
||||
for _, userID := range req.UserIDList {
|
||||
platformList := genPlatformArray()
|
||||
temp := new(pbRelay.GetUsersOnlineStatusResp_SuccessResult)
|
||||
temp.UserID = v1
|
||||
for _, v2 := range userIDList {
|
||||
UIDAndPID = strings.Split(v2, " ")
|
||||
if conn := ws.getUserConn(v2); conn != nil {
|
||||
temp.UserID = userID
|
||||
for _, platform := range platformList {
|
||||
if conn := ws.getUserConn(userID, platform); conn != nil {
|
||||
ps := new(pbRelay.GetUsersOnlineStatusResp_SuccessDetail)
|
||||
ps.Platform = UIDAndPID[1]
|
||||
ps.Platform = platform
|
||||
ps.Status = constant.OnlineStatus
|
||||
temp.Status = constant.OnlineStatus
|
||||
temp.DetailPlatformStatus = append(temp.DetailPlatformStatus, ps)
|
||||
@@ -157,11 +127,11 @@ func (r *RPCServer) GetUsersOnlineStatus(_ context.Context, req *pbRelay.GetUser
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
func sendMsgToUser(conn *UserConn, bMsg []byte, in *pbRelay.MsgToUserReq, RecvPlatForm, RecvID string) (ResultCode int64) {
|
||||
func sendMsgToUser(conn *UserConn, bMsg []byte, in *pbRelay.OnlinePushMsgReq, RecvPlatForm, RecvID string) (ResultCode int64) {
|
||||
err := ws.writeMsg(conn, websocket.BinaryMessage, bMsg)
|
||||
if err != nil {
|
||||
log.ErrorByKv("PushMsgToUser is failed By Ws", "", "Addr", conn.RemoteAddr().String(),
|
||||
"error", err, "senderPlatform", constant.PlatformIDToName(in.PlatformID), "recvPlatform", RecvPlatForm, "args", in.String(), "recvID", RecvID)
|
||||
"error", err, "senderPlatform", constant.PlatformIDToName(in.MsgData.SenderPlatformID), "recvPlatform", RecvPlatForm, "args", in.String(), "recvID", RecvID)
|
||||
ResultCode = -2
|
||||
return ResultCode
|
||||
} else {
|
||||
@@ -171,9 +141,9 @@ func sendMsgToUser(conn *UserConn, bMsg []byte, in *pbRelay.MsgToUserReq, RecvPl
|
||||
}
|
||||
|
||||
}
|
||||
func genUidPlatformArray(uid string) (array []string) {
|
||||
func genPlatformArray() (array []string) {
|
||||
for i := 1; i <= constant.LinuxPlatformID; i++ {
|
||||
array = append(array, uid+" "+constant.PlatformIDToName(int32(i)))
|
||||
array = append(array, constant.PlatformIDToName(int32(i)))
|
||||
}
|
||||
return array
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"Open_IM/pkg/utils"
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -24,15 +25,15 @@ type WServer struct {
|
||||
wsAddr string
|
||||
wsMaxConnNum int
|
||||
wsUpGrader *websocket.Upgrader
|
||||
wsConnToUser map[*UserConn]string
|
||||
wsUserToConn map[string]*UserConn
|
||||
wsConnToUser map[*UserConn]map[string]string
|
||||
wsUserToConn map[string]map[string]*UserConn
|
||||
}
|
||||
|
||||
func (ws *WServer) onInit(wsPort int) {
|
||||
ws.wsAddr = ":" + utils.IntToString(wsPort)
|
||||
ws.wsMaxConnNum = config.Config.LongConnSvr.WebsocketMaxConnNum
|
||||
ws.wsConnToUser = make(map[*UserConn]string)
|
||||
ws.wsUserToConn = make(map[string]*UserConn)
|
||||
ws.wsConnToUser = make(map[*UserConn]map[string]string)
|
||||
ws.wsUserToConn = make(map[string]map[string]*UserConn)
|
||||
ws.wsUpGrader = &websocket.Upgrader{
|
||||
HandshakeTimeout: time.Duration(config.Config.LongConnSvr.WebsocketTimeOut) * time.Second,
|
||||
ReadBufferSize: config.Config.LongConnSvr.WebsocketMaxMsgLen,
|
||||
@@ -74,7 +75,8 @@ func (ws *WServer) readMsg(conn *UserConn) {
|
||||
log.NewInfo("", "this is a pingMessage")
|
||||
}
|
||||
if err != nil {
|
||||
log.ErrorByKv("WS ReadMsg error", "", "userIP", conn.RemoteAddr().String(), "userUid", ws.getUserUid(conn), "error", err)
|
||||
uid, platform := ws.getUserUid(conn)
|
||||
log.ErrorByKv("WS ReadMsg error", "", "userIP", conn.RemoteAddr().String(), "userUid", uid, "platform", platform, "error", err.Error())
|
||||
ws.delUserConn(conn)
|
||||
return
|
||||
} else {
|
||||
@@ -94,34 +96,43 @@ func (ws *WServer) writeMsg(conn *UserConn, a int, msg []byte) error {
|
||||
func (ws *WServer) MultiTerminalLoginChecker(uid string, platformID int32, newConn *UserConn, token string) {
|
||||
switch config.Config.MultiLoginPolicy {
|
||||
case constant.AllLoginButSameTermKick:
|
||||
if oldConn, ok := ws.wsUserToConn[genMapKey(uid, platformID)]; ok {
|
||||
log.NewDebug("", uid, platformID, "kick old conn")
|
||||
ws.sendKickMsg(oldConn, newConn)
|
||||
m, err := db.DB.GetTokenMapByUidPid(uid, constant.PlatformIDToName(platformID))
|
||||
if err != nil {
|
||||
log.NewError("", "get token from redis err", err.Error())
|
||||
return
|
||||
}
|
||||
if m == nil {
|
||||
log.NewError("", "get token from redis err", "m is nil")
|
||||
return
|
||||
}
|
||||
for k, _ := range m {
|
||||
if k != token {
|
||||
m[k] = constant.KickedToken
|
||||
if oldConnMap, ok := ws.wsUserToConn[uid]; ok {
|
||||
if oldConn, ok := oldConnMap[constant.PlatformIDToName(platformID)]; ok {
|
||||
log.NewDebug("", uid, platformID, "kick old conn")
|
||||
ws.sendKickMsg(oldConn, newConn)
|
||||
m, err := db.DB.GetTokenMapByUidPid(uid, constant.PlatformIDToName(platformID))
|
||||
if err != nil && err != redis.ErrNil {
|
||||
log.NewError("", "get token from redis err", err.Error())
|
||||
return
|
||||
}
|
||||
if m == nil {
|
||||
log.NewError("", "get token from redis err", "m is nil")
|
||||
return
|
||||
}
|
||||
for k, _ := range m {
|
||||
if k != token {
|
||||
m[k] = constant.KickedToken
|
||||
}
|
||||
}
|
||||
log.NewDebug("get map is ", m)
|
||||
err = db.DB.SetTokenMapByUidPid(uid, platformID, m)
|
||||
if err != nil {
|
||||
log.NewError("", "SetTokenMapByUidPid err", err.Error())
|
||||
return
|
||||
}
|
||||
err = oldConn.Close()
|
||||
delete(oldConnMap, constant.PlatformIDToName(platformID))
|
||||
ws.wsUserToConn[uid] = oldConnMap
|
||||
if len(oldConnMap) == 0 {
|
||||
delete(ws.wsUserToConn, uid)
|
||||
}
|
||||
delete(ws.wsConnToUser, oldConn)
|
||||
if err != nil {
|
||||
log.NewError("", "conn close err", err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
log.NewDebug("get map is ", m)
|
||||
err = db.DB.SetTokenMapByUidPid(uid, platformID, m)
|
||||
if err != nil {
|
||||
log.NewError("", "SetTokenMapByUidPid err", err.Error())
|
||||
return
|
||||
}
|
||||
err = oldConn.Close()
|
||||
delete(ws.wsConnToUser, oldConn)
|
||||
if err != nil {
|
||||
log.NewError("", "conn close err", err.Error())
|
||||
}
|
||||
|
||||
} else {
|
||||
log.NewDebug("no other conn", ws.wsUserToConn)
|
||||
}
|
||||
@@ -149,53 +160,97 @@ func (ws *WServer) sendKickMsg(oldConn, newConn *UserConn) {
|
||||
}
|
||||
}
|
||||
func (ws *WServer) addUserConn(uid string, platformID int32, conn *UserConn, token string) {
|
||||
key := genMapKey(uid, platformID)
|
||||
rwLock.Lock()
|
||||
defer rwLock.Unlock()
|
||||
ws.MultiTerminalLoginChecker(uid, platformID, conn, token)
|
||||
ws.wsConnToUser[conn] = key
|
||||
ws.wsUserToConn[key] = conn
|
||||
log.WarnByKv("WS Add operation", "", "wsUser added", ws.wsUserToConn, "uid", uid, "online_num", len(ws.wsUserToConn))
|
||||
if oldConnMap, ok := ws.wsUserToConn[uid]; ok {
|
||||
oldConnMap[constant.PlatformIDToName(platformID)] = conn
|
||||
ws.wsUserToConn[uid] = oldConnMap
|
||||
} else {
|
||||
i := make(map[string]*UserConn)
|
||||
i[constant.PlatformIDToName(platformID)] = conn
|
||||
ws.wsUserToConn[uid] = i
|
||||
}
|
||||
if oldStringMap, ok := ws.wsConnToUser[conn]; ok {
|
||||
oldStringMap[constant.PlatformIDToName(platformID)] = uid
|
||||
ws.wsConnToUser[conn] = oldStringMap
|
||||
} else {
|
||||
i := make(map[string]string)
|
||||
i[constant.PlatformIDToName(platformID)] = uid
|
||||
ws.wsConnToUser[conn] = i
|
||||
}
|
||||
count := 0
|
||||
for _, v := range ws.wsUserToConn {
|
||||
count = count + len(v)
|
||||
}
|
||||
log.WarnByKv("WS Add operation", "", "wsUser added", ws.wsUserToConn, "uid", uid, "online_user_num", len(ws.wsUserToConn), "online_conn_num", count)
|
||||
|
||||
}
|
||||
|
||||
func (ws *WServer) delUserConn(conn *UserConn) {
|
||||
rwLock.Lock()
|
||||
defer rwLock.Unlock()
|
||||
var uidPlatform string
|
||||
if uid, ok := ws.wsConnToUser[conn]; ok {
|
||||
uidPlatform = uid
|
||||
if _, ok = ws.wsUserToConn[uid]; ok {
|
||||
delete(ws.wsUserToConn, uid)
|
||||
log.WarnByKv("WS delete operation", "", "wsUser deleted", ws.wsUserToConn, "uid", uid, "online_num", len(ws.wsUserToConn))
|
||||
var platform, uid string
|
||||
if oldStringMap, ok := ws.wsConnToUser[conn]; ok {
|
||||
for k, v := range oldStringMap {
|
||||
platform = k
|
||||
uid = v
|
||||
}
|
||||
if oldConnMap, ok := ws.wsUserToConn[uid]; ok {
|
||||
delete(oldConnMap, platform)
|
||||
ws.wsUserToConn[uid] = oldConnMap
|
||||
if len(oldConnMap) == 0 {
|
||||
delete(ws.wsUserToConn, uid)
|
||||
}
|
||||
count := 0
|
||||
for _, v := range ws.wsUserToConn {
|
||||
count = count + len(v)
|
||||
}
|
||||
log.WarnByKv("WS delete operation", "", "wsUser deleted", ws.wsUserToConn, "uid", uid, "online_user_num", len(ws.wsUserToConn), "online_conn_num", count)
|
||||
} else {
|
||||
log.WarnByKv("uid not exist", "", "wsUser deleted", ws.wsUserToConn, "uid", uid, "online_num", len(ws.wsUserToConn))
|
||||
log.WarnByKv("WS delete operation", "", "wsUser deleted", ws.wsUserToConn, "uid", uid, "online_user_num", len(ws.wsUserToConn))
|
||||
}
|
||||
delete(ws.wsConnToUser, conn)
|
||||
|
||||
}
|
||||
err := conn.Close()
|
||||
if err != nil {
|
||||
log.ErrorByKv("close err", "", "uid", uidPlatform)
|
||||
log.ErrorByKv("close err", "", "uid", uid, "platform", platform)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ws *WServer) getUserConn(uid string) *UserConn {
|
||||
func (ws *WServer) getUserConn(uid string, platform string) *UserConn {
|
||||
rwLock.RLock()
|
||||
defer rwLock.RUnlock()
|
||||
if conn, ok := ws.wsUserToConn[uid]; ok {
|
||||
return conn
|
||||
if connMap, ok := ws.wsUserToConn[uid]; ok {
|
||||
if conn, flag := connMap[platform]; flag {
|
||||
return conn
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (ws *WServer) getUserUid(conn *UserConn) string {
|
||||
func (ws *WServer) getSingleUserAllConn(uid string) map[string]*UserConn {
|
||||
rwLock.RLock()
|
||||
defer rwLock.RUnlock()
|
||||
if connMap, ok := ws.wsUserToConn[uid]; ok {
|
||||
return connMap
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (ws *WServer) getUserUid(conn *UserConn) (uid, platform string) {
|
||||
rwLock.RLock()
|
||||
defer rwLock.RUnlock()
|
||||
|
||||
if uid, ok := ws.wsConnToUser[conn]; ok {
|
||||
return uid
|
||||
if stringMap, ok := ws.wsConnToUser[conn]; ok {
|
||||
for k, v := range stringMap {
|
||||
platform = k
|
||||
uid = v
|
||||
}
|
||||
return uid, platform
|
||||
}
|
||||
return ""
|
||||
return "", ""
|
||||
}
|
||||
func (ws *WServer) headerCheck(w http.ResponseWriter, r *http.Request) bool {
|
||||
status := http.StatusUnauthorized
|
||||
|
||||
Reference in New Issue
Block a user