You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/msggateway/n_ws_server.go

211 lines
5.4 KiB

2 years ago
package msggateway
import (
"errors"
"fmt"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
"github.com/go-playground/validator/v10"
"net/http"
"sync"
2 years ago
"sync/atomic"
"time"
)
2 years ago
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
}
2 years ago
var bufferPool = sync.Pool{
New: func() interface{} {
2 years ago
return make([]byte, 1024)
2 years ago
},
}
type WsServer struct {
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
2 years ago
validate *validator.Validate
2 years ago
Compressor
Encoder
MessageHandler
}
func (ws *WsServer) UnRegister(c *Client) {
ws.unregisterChan <- c
}
func (ws *WsServer) Validate(s interface{}) error {
2 years ago
return nil
2 years ago
}
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)
}
2 years ago
func NewWsServer(opts ...Option) (*WsServer, error) {
var config configs
for _, o := range opts {
o(&config)
}
if config.port < 1024 {
return nil, errors.New("port not allow to listen")
}
2 years ago
v := validator.New()
return &WsServer{
port: config.port,
wsMaxConnNum: config.maxConnNum,
handshakeTimeout: config.handshakeTimeout,
readBufferSize: config.messageMaxMsgLength,
clientPool: sync.Pool{
New: func() interface{} {
return new(Client)
},
},
2 years ago
registerChan: make(chan *Client, 1000),
unregisterChan: make(chan *Client, 1000),
validate: v,
clients: newUserMap(),
Compressor: NewGzipCompressor(),
Encoder: NewGobEncoder(),
MessageHandler: NewGrpcHandler(v, nil),
2 years ago
//handler: NewGrpcHandler(validate),
}, nil
}
func (ws *WsServer) Run() error {
2 years ago
var client *Client
go func() {
for {
select {
case client = <-ws.registerChan:
ws.registerClient(client)
case client = <-ws.unregisterChan:
ws.unregisterClient(client)
2 years ago
}
}
}()
2 years ago
http.HandleFunc("/", ws.wsHandler) //Get request from client to handle by wsHandler
return http.ListenAndServe(":"+utils.IntToString(ws.port), nil) //Start listening
2 years ago
}
func (ws *WsServer) registerClient(client *Client) {
var (
2 years ago
userOK bool
clientOK bool
2 years ago
cli []*Client
2 years ago
)
2 years ago
cli, userOK, clientOK = ws.clients.Get(client.userID, client.platformID)
if !userOK {
ws.clients.Set(client.userID, client)
atomic.AddInt64(&ws.onlineUserNum, 1)
atomic.AddInt64(&ws.onlineUserConnNum, 1)
fmt.Println("R在线用户数量:", ws.onlineUserNum)
fmt.Println("R在线用户连接数量:", ws.onlineUserConnNum)
2 years ago
} else {
if clientOK { //已经有同平台的连接存在
ws.clients.Set(client.userID, client)
ws.multiTerminalLoginChecker(cli)
2 years ago
} else {
ws.clients.Set(client.userID, client)
atomic.AddInt64(&ws.onlineUserConnNum, 1)
fmt.Println("R在线用户数量:", ws.onlineUserNum)
fmt.Println("R在线用户连接数量:", ws.onlineUserConnNum)
}
2 years ago
}
}
2 years ago
func (ws *WsServer) multiTerminalLoginChecker(client []*Client) {
}
func (ws *WsServer) unregisterClient(client *Client) {
2 years ago
defer ws.clientPool.Put(client)
2 years ago
isDeleteUser := ws.clients.delete(client.userID, client.platformID)
if isDeleteUser {
atomic.AddInt64(&ws.onlineUserNum, -1)
2 years ago
}
atomic.AddInt64(&ws.onlineUserConnNum, -1)
fmt.Println("R在线用户数量:", ws.onlineUserNum)
fmt.Println("R在线用户连接数量:", ws.onlineUserConnNum)
2 years ago
}
func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
context := newContext(w, r)
if ws.onlineUserConnNum >= ws.wsMaxConnNum {
2 years ago
httpError(context, errs.ErrConnOverMaxNumLimit)
return
}
var (
2 years ago
token string
userID string
platformID string
exists bool
compression bool
)
2 years ago
token, exists = context.Query(Token)
if !exists {
2 years ago
httpError(context, errs.ErrConnArgsErr)
return
}
2 years ago
userID, exists = context.Query(WsUserID)
if !exists {
2 years ago
httpError(context, errs.ErrConnArgsErr)
return
}
2 years ago
platformID, exists = context.Query(PlatformID)
if !exists {
2 years ago
httpError(context, errs.ErrConnArgsErr)
return
}
err := tokenverify.WsVerifyToken(token, userID, platformID)
if err != nil {
httpError(context, err)
return
}
2 years ago
wsLongConn := newGWebSocket(WebSocket, ws.handshakeTimeout, ws.readBufferSize)
err = wsLongConn.GenerateLongConn(w, r)
if err != nil {
httpError(context, err)
return
}
2 years ago
compressProtoc, exists := context.Query(Compression)
if exists {
2 years ago
if compressProtoc == GzipCompressionProtocol {
compression = true
}
}
2 years ago
compressProtoc, exists = context.GetHeader(Compression)
if exists {
2 years ago
if compressProtoc == GzipCompressionProtocol {
compression = true
}
}
2 years ago
client := ws.clientPool.Get().(*Client)
2 years ago
client.ResetClient(context, wsLongConn, compression, ws)
ws.registerChan <- client
go client.readMessage()
}