|
|
|
@ -13,6 +13,7 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
|
|
|
|
|
redis "github.com/go-redis/redis/v8"
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
|
|
|
|
@ -141,9 +142,9 @@ func (ws *WsServer) registerClient(client *Client) {
|
|
|
|
|
clientOK bool
|
|
|
|
|
oldClients []*Client
|
|
|
|
|
)
|
|
|
|
|
ws.clients.Set(client.UserID, client)
|
|
|
|
|
oldClients, userOK, clientOK = ws.clients.Get(client.UserID, client.PlatformID)
|
|
|
|
|
if !userOK {
|
|
|
|
|
ws.clients.Set(client.UserID, client)
|
|
|
|
|
log.ZDebug(client.ctx, "user not exist", "userID", client.UserID, "platformID", client.PlatformID)
|
|
|
|
|
atomic.AddInt64(&ws.onlineUserNum, 1)
|
|
|
|
|
atomic.AddInt64(&ws.onlineUserConnNum, 1)
|
|
|
|
@ -156,10 +157,14 @@ func (ws *WsServer) registerClient(client *Client) {
|
|
|
|
|
}
|
|
|
|
|
ws.kickHandlerChan <- i
|
|
|
|
|
log.ZDebug(client.ctx, "user exist", "userID", client.UserID, "platformID", client.PlatformID)
|
|
|
|
|
if clientOK { //已经有同平台的连接存在
|
|
|
|
|
if clientOK {
|
|
|
|
|
ws.clients.Set(client.UserID, client)
|
|
|
|
|
//已经有同平台的连接存在
|
|
|
|
|
log.ZInfo(client.ctx, "repeat login", "userID", client.UserID, "platformID", client.PlatformID, "old remote addr", getRemoteAdders(oldClients))
|
|
|
|
|
atomic.AddInt64(&ws.onlineUserConnNum, 1)
|
|
|
|
|
} else {
|
|
|
|
|
ws.clients.Set(client.UserID, client)
|
|
|
|
|
|
|
|
|
|
atomic.AddInt64(&ws.onlineUserConnNum, 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -187,13 +192,35 @@ func (ws *WsServer) multiTerminalLoginChecker(info *kickHandler) {
|
|
|
|
|
fallthrough
|
|
|
|
|
case constant.AllLoginButSameTermKick:
|
|
|
|
|
if info.clientOK {
|
|
|
|
|
ws.clients.deleteClients(info.newClient.UserID, info.oldClients)
|
|
|
|
|
for _, c := range info.oldClients {
|
|
|
|
|
err := c.KickOnlineMessage()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.ZError(c.ctx, "KickOnlineMessage", err)
|
|
|
|
|
log.ZWarn(c.ctx, "KickOnlineMessage", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m, err := ws.cache.GetTokensWithoutError(info.newClient.ctx, info.newClient.UserID, info.newClient.PlatformID)
|
|
|
|
|
if err != nil && err != redis.Nil {
|
|
|
|
|
log.ZWarn(info.newClient.ctx, "get token from redis err", err, "userID", info.newClient.UserID, "platformID", info.newClient.PlatformID)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if m == nil {
|
|
|
|
|
log.ZWarn(info.newClient.ctx, "m is nil", errors.New("m is nil"), "userID", info.newClient.UserID, "platformID", info.newClient.PlatformID)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.ZDebug(info.newClient.ctx, "get token from redis", "userID", info.newClient.UserID, "platformID", info.newClient.PlatformID, "tokenMap", m)
|
|
|
|
|
|
|
|
|
|
for k, _ := range m {
|
|
|
|
|
if k != info.newClient.ctx.GetToken() {
|
|
|
|
|
m[k] = constant.KickedToken
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
log.ZDebug(info.newClient.ctx, "set token map is ", "token map", m, "userID", info.newClient.UserID)
|
|
|
|
|
err = ws.cache.SetTokenMapByUidPid(info.newClient.ctx, info.newClient.UserID, info.newClient.PlatformID, m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.ZWarn(info.newClient.ctx, "SetTokenMapByUidPid err", err, "userID", info.newClient.UserID, "platformID", info.newClient.PlatformID)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ws.cache.GetTokensWithoutError(info.newClient.ctx, info.newClient.UserID, info.newClient.PlatformID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -209,7 +236,6 @@ func (ws *WsServer) unregisterClient(client *Client) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
defer log.ZInfo(context.Background(), "wsHandler", "remote addr", "url", r.URL.String())
|
|
|
|
|
connContext := newContext(w, r)
|
|
|
|
|
if ws.onlineUserConnNum >= ws.wsMaxConnNum {
|
|
|
|
|
httpError(connContext, errs.ErrConnOverMaxNumLimit)
|
|
|
|
|