Files
open-im-server/internal/msggateway/n_ws_server.go
T

218 lines
5.7 KiB
Go
Raw Normal View History

2023-03-08 18:39:18 +08:00
package msggateway
2023-02-14 21:08:36 +08:00
import (
"errors"
2023-02-16 16:32:31 +08:00
"fmt"
2023-03-23 13:17:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
2023-03-23 12:05:25 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2023-02-16 16:32:31 +08:00
"github.com/go-playground/validator/v10"
2023-02-14 21:08:36 +08:00
"net/http"
"sync"
2023-02-15 19:57:16 +08:00
"sync/atomic"
2023-02-14 21:08:36 +08:00
"time"
)
2023-03-08 18:39:18 +08:00
type LongConnServer interface {
Run() error
wsHandler(w http.ResponseWriter, r *http.Request)
GetUserAllCons(userID string) ([]*Client, bool)
GetUserPlatformCons(userID string, platform int) ([]*Client, bool, bool)
Validate(s interface{}) error
UnRegister(c *Client)
Compressor
Encoder
MessageHandler
}
2023-02-15 19:57:16 +08:00
var bufferPool = sync.Pool{
New: func() interface{} {
2023-02-22 21:06:55 +08:00
return make([]byte, 1024)
2023-02-15 19:57:16 +08:00
},
}
2023-02-16 16:32:31 +08:00
2023-02-14 21:08:36 +08:00
type WsServer struct {
2023-02-16 16:32:31 +08:00
port int
wsMaxConnNum int64
registerChan chan *Client
unregisterChan chan *Client
clients *UserMap
clientPool sync.Pool
onlineUserNum int64
onlineUserConnNum int64
handshakeTimeout time.Duration
readBufferSize, WriteBufferSize int
2023-03-23 12:05:25 +08:00
hubServer *Server
2023-02-22 21:06:55 +08:00
validate *validator.Validate
2023-03-08 18:39:18 +08:00
Compressor
Encoder
MessageHandler
}
2023-03-23 12:05:25 +08:00
func (ws *WsServer) SetMessageHandler(rpcClient *notification.Check) {
2023-03-23 13:17:06 +08:00
log.ZDebug(nil, "msggateway SetMessageHandler", "rpcClient", rpcClient)
2023-03-23 12:05:25 +08:00
ws.MessageHandler = NewGrpcHandler(ws.validate, rpcClient)
}
2023-03-08 18:39:18 +08:00
func (ws *WsServer) UnRegister(c *Client) {
ws.unregisterChan <- c
}
func (ws *WsServer) Validate(s interface{}) error {
2023-03-15 14:38:11 +08:00
return nil
2023-03-08 18:39:18 +08:00
}
func (ws *WsServer) GetUserAllCons(userID string) ([]*Client, bool) {
return ws.clients.GetAll(userID)
}
func (ws *WsServer) GetUserPlatformCons(userID string, platform int) ([]*Client, bool, bool) {
return ws.clients.Get(userID, platform)
2023-02-14 21:08:36 +08:00
}
2023-03-08 18:39:18 +08:00
func NewWsServer(opts ...Option) (*WsServer, error) {
2023-02-14 21:08:36 +08:00
var config configs
for _, o := range opts {
o(&config)
}
if config.port < 1024 {
return nil, errors.New("port not allow to listen")
}
2023-03-08 18:39:18 +08:00
v := validator.New()
2023-02-14 21:08:36 +08:00
return &WsServer{
2023-02-16 16:32:31 +08:00
port: config.port,
wsMaxConnNum: config.maxConnNum,
handshakeTimeout: config.handshakeTimeout,
readBufferSize: config.messageMaxMsgLength,
2023-02-14 21:08:36 +08:00
clientPool: sync.Pool{
New: func() interface{} {
return new(Client)
},
},
2023-03-08 18:39:18 +08:00
registerChan: make(chan *Client, 1000),
unregisterChan: make(chan *Client, 1000),
validate: v,
clients: newUserMap(),
Compressor: NewGzipCompressor(),
Encoder: NewGobEncoder(),
2023-02-22 21:06:55 +08:00
//handler: NewGrpcHandler(validate),
2023-02-14 21:08:36 +08:00
}, nil
}
func (ws *WsServer) Run() error {
2023-02-15 19:57:16 +08:00
var client *Client
go func() {
for {
select {
case client = <-ws.registerChan:
ws.registerClient(client)
2023-02-16 16:32:31 +08:00
case client = <-ws.unregisterChan:
ws.unregisterClient(client)
2023-02-15 19:57:16 +08:00
}
}
}()
2023-02-22 21:06:55 +08:00
http.HandleFunc("/", ws.wsHandler) //Get request from client to handle by wsHandler
2023-02-16 16:32:31 +08:00
return http.ListenAndServe(":"+utils.IntToString(ws.port), nil) //Start listening
2023-02-15 19:57:16 +08:00
}
func (ws *WsServer) registerClient(client *Client) {
var (
2023-02-22 21:06:55 +08:00
userOK bool
2023-02-16 16:32:31 +08:00
clientOK bool
2023-03-08 18:39:18 +08:00
cli []*Client
2023-02-15 19:57:16 +08:00
)
2023-02-22 21:06:55 +08:00
cli, userOK, clientOK = ws.clients.Get(client.userID, client.platformID)
if !userOK {
ws.clients.Set(client.userID, client)
2023-02-16 16:32:31 +08:00
atomic.AddInt64(&ws.onlineUserNum, 1)
atomic.AddInt64(&ws.onlineUserConnNum, 1)
fmt.Println("R在线用户数量:", ws.onlineUserNum)
fmt.Println("R在线用户连接数量:", ws.onlineUserConnNum)
2023-02-22 21:06:55 +08:00
} else {
if clientOK { //已经有同平台的连接存在
ws.clients.Set(client.userID, client)
2023-02-16 16:32:31 +08:00
ws.multiTerminalLoginChecker(cli)
2023-02-22 21:06:55 +08:00
} else {
ws.clients.Set(client.userID, client)
2023-02-16 16:32:31 +08:00
atomic.AddInt64(&ws.onlineUserConnNum, 1)
fmt.Println("R在线用户数量:", ws.onlineUserNum)
fmt.Println("R在线用户连接数量:", ws.onlineUserConnNum)
}
2023-02-15 19:57:16 +08:00
}
2023-02-16 16:32:31 +08:00
}
2023-03-08 18:39:18 +08:00
func (ws *WsServer) multiTerminalLoginChecker(client []*Client) {
2023-02-16 16:32:31 +08:00
}
func (ws *WsServer) unregisterClient(client *Client) {
2023-03-08 18:39:18 +08:00
defer ws.clientPool.Put(client)
2023-02-22 21:06:55 +08:00
isDeleteUser := ws.clients.delete(client.userID, client.platformID)
if isDeleteUser {
2023-02-16 16:32:31 +08:00
atomic.AddInt64(&ws.onlineUserNum, -1)
2023-02-15 19:57:16 +08:00
}
2023-02-16 16:32:31 +08:00
atomic.AddInt64(&ws.onlineUserConnNum, -1)
fmt.Println("R在线用户数量:", ws.onlineUserNum)
fmt.Println("R在线用户连接数量:", ws.onlineUserConnNum)
2023-02-15 19:57:16 +08:00
}
2023-02-16 16:32:31 +08:00
2023-02-14 21:08:36 +08:00
func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
context := newContext(w, r)
2023-02-16 16:32:31 +08:00
if ws.onlineUserConnNum >= ws.wsMaxConnNum {
2023-03-07 12:19:30 +08:00
httpError(context, errs.ErrConnOverMaxNumLimit)
2023-02-16 16:32:31 +08:00
return
}
var (
2023-02-22 21:06:55 +08:00
token string
userID string
platformID string
exists bool
compression bool
2023-02-16 16:32:31 +08:00
)
2023-03-08 18:39:18 +08:00
token, exists = context.Query(Token)
2023-02-16 16:32:31 +08:00
if !exists {
2023-03-07 12:19:30 +08:00
httpError(context, errs.ErrConnArgsErr)
2023-02-16 16:32:31 +08:00
return
}
2023-03-08 18:39:18 +08:00
userID, exists = context.Query(WsUserID)
2023-02-16 16:32:31 +08:00
if !exists {
2023-03-07 12:19:30 +08:00
httpError(context, errs.ErrConnArgsErr)
2023-02-16 16:32:31 +08:00
return
}
2023-03-08 18:39:18 +08:00
platformID, exists = context.Query(PlatformID)
2023-02-16 16:32:31 +08:00
if !exists {
2023-03-07 12:19:30 +08:00
httpError(context, errs.ErrConnArgsErr)
2023-02-16 16:32:31 +08:00
return
}
err := tokenverify.WsVerifyToken(token, userID, platformID)
if err != nil {
httpError(context, err)
return
}
2023-03-08 18:39:18 +08:00
wsLongConn := newGWebSocket(WebSocket, ws.handshakeTimeout, ws.readBufferSize)
2023-02-16 16:32:31 +08:00
err = wsLongConn.GenerateLongConn(w, r)
if err != nil {
httpError(context, err)
return
}
2023-03-08 18:39:18 +08:00
compressProtoc, exists := context.Query(Compression)
2023-02-16 16:32:31 +08:00
if exists {
2023-03-08 18:39:18 +08:00
if compressProtoc == GzipCompressionProtocol {
2023-02-16 16:32:31 +08:00
compression = true
}
}
2023-03-08 18:39:18 +08:00
compressProtoc, exists = context.GetHeader(Compression)
2023-02-16 16:32:31 +08:00
if exists {
2023-03-08 18:39:18 +08:00
if compressProtoc == GzipCompressionProtocol {
2023-02-16 16:32:31 +08:00
compression = true
2023-02-14 21:08:36 +08:00
}
}
2023-02-22 21:06:55 +08:00
client := ws.clientPool.Get().(*Client)
2023-03-08 18:39:18 +08:00
client.ResetClient(context, wsLongConn, compression, ws)
2023-02-16 16:32:31 +08:00
ws.registerChan <- client
go client.readMessage()
2023-02-14 21:08:36 +08:00
}