mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
feat: support websocket first message method response code (#1651)
* upgrade package and rtc convert * upgrade package and rtc convert * upgrade package and rtc convert * upgrade package and rtc convert * friend user * s3 form data * s3 form data * s3 form data * s3 form data * s3 form data * s3 form data * s3 form data * s3 form data * s3 form data * ws * ws * ws * ws * ws
This commit is contained in:
parent
49f4e3f0de
commit
3fffc2f212
@ -26,6 +26,7 @@ const (
|
|||||||
Compression = "compression"
|
Compression = "compression"
|
||||||
GzipCompressionProtocol = "gzip"
|
GzipCompressionProtocol = "gzip"
|
||||||
BackgroundStatus = "isBackground"
|
BackgroundStatus = "isBackground"
|
||||||
|
MsgResp = "isMsgResp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -16,7 +16,10 @@ package msggateway
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/OpenIMSDK/tools/apiresp"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -422,84 +425,102 @@ func (ws *WsServer) unregisterClient(client *Client) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
|
func (ws *WsServer) ParseWSArgs(r *http.Request) (args *WSArgs, err error) {
|
||||||
connContext := newContext(w, r)
|
var v WSArgs
|
||||||
|
defer func() {
|
||||||
|
args = &v
|
||||||
|
}()
|
||||||
|
query := r.URL.Query()
|
||||||
|
v.MsgResp, _ = strconv.ParseBool(query.Get(MsgResp))
|
||||||
if ws.onlineUserConnNum.Load() >= ws.wsMaxConnNum {
|
if ws.onlineUserConnNum.Load() >= ws.wsMaxConnNum {
|
||||||
httpError(connContext, errs.ErrConnOverMaxNumLimit)
|
return nil, errs.ErrConnOverMaxNumLimit.Wrap("over max conn num limit")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
var (
|
if v.Token = query.Get(Token); v.Token == "" {
|
||||||
token string
|
return nil, errs.ErrConnArgsErr.Wrap("token is empty")
|
||||||
userID string
|
|
||||||
platformIDStr string
|
|
||||||
exists bool
|
|
||||||
compression bool
|
|
||||||
)
|
|
||||||
|
|
||||||
token, exists = connContext.Query(Token)
|
|
||||||
if !exists {
|
|
||||||
httpError(connContext, errs.ErrConnArgsErr)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
userID, exists = connContext.Query(WsUserID)
|
if v.UserID = query.Get(WsUserID); v.UserID == "" {
|
||||||
if !exists {
|
return nil, errs.ErrConnArgsErr.Wrap("sendID is empty")
|
||||||
httpError(connContext, errs.ErrConnArgsErr)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
platformIDStr, exists = connContext.Query(PlatformID)
|
platformIDStr := query.Get(PlatformID)
|
||||||
if !exists {
|
if platformIDStr == "" {
|
||||||
httpError(connContext, errs.ErrConnArgsErr)
|
return nil, errs.ErrConnArgsErr.Wrap("platformID is empty")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
platformID, err := strconv.Atoi(platformIDStr)
|
platformID, err := strconv.Atoi(platformIDStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(connContext, errs.ErrConnArgsErr)
|
return nil, errs.ErrConnArgsErr.Wrap("platformID is not int")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if err = authverify.WsVerifyToken(token, userID, platformID); err != nil {
|
v.PlatformID = platformID
|
||||||
httpError(connContext, err)
|
if err = authverify.WsVerifyToken(v.Token, v.UserID, platformID); err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
m, err := ws.cache.GetTokensWithoutError(context.Background(), userID, platformID)
|
if query.Get(Compression) == GzipCompressionProtocol {
|
||||||
|
v.Compression = true
|
||||||
|
}
|
||||||
|
if r.Header.Get(Compression) == GzipCompressionProtocol {
|
||||||
|
v.Compression = true
|
||||||
|
}
|
||||||
|
m, err := ws.cache.GetTokensWithoutError(context.Background(), v.UserID, platformID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(connContext, err)
|
return nil, err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
if v, ok := m[token]; ok {
|
if v, ok := m[v.Token]; ok {
|
||||||
switch v {
|
switch v {
|
||||||
case constant.NormalToken:
|
case constant.NormalToken:
|
||||||
case constant.KickedToken:
|
case constant.KickedToken:
|
||||||
httpError(connContext, errs.ErrTokenKicked.Wrap())
|
return nil, errs.ErrTokenKicked.Wrap()
|
||||||
return
|
|
||||||
default:
|
default:
|
||||||
httpError(connContext, errs.ErrTokenUnknown.Wrap())
|
return nil, errs.ErrTokenUnknown.Wrap(fmt.Sprintf("token status is %d", v))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return nil, errs.ErrTokenNotExist.Wrap()
|
||||||
|
}
|
||||||
|
return &v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type WSArgs struct {
|
||||||
|
Token string
|
||||||
|
UserID string
|
||||||
|
PlatformID int
|
||||||
|
Compression bool
|
||||||
|
MsgResp bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
connContext := newContext(w, r)
|
||||||
|
args, pErr := ws.ParseWSArgs(r)
|
||||||
|
var wsLongConn *GWebSocket
|
||||||
|
if args.MsgResp {
|
||||||
|
wsLongConn = newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
|
||||||
|
if err := wsLongConn.GenerateLongConn(w, r); err != nil {
|
||||||
|
httpError(connContext, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(apiresp.ParseError(pErr))
|
||||||
|
if err != nil {
|
||||||
|
_ = wsLongConn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := wsLongConn.WriteMessage(MessageText, data); err != nil {
|
||||||
|
_ = wsLongConn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pErr != nil {
|
||||||
|
_ = wsLongConn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
httpError(connContext, errs.ErrTokenNotExist.Wrap())
|
if pErr != nil {
|
||||||
return
|
httpError(connContext, pErr)
|
||||||
}
|
return
|
||||||
|
|
||||||
wsLongConn := newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
|
|
||||||
err = wsLongConn.GenerateLongConn(w, r)
|
|
||||||
if err != nil {
|
|
||||||
httpError(connContext, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
compressProtoc, exists := connContext.Query(Compression)
|
|
||||||
if exists {
|
|
||||||
if compressProtoc == GzipCompressionProtocol {
|
|
||||||
compression = true
|
|
||||||
}
|
}
|
||||||
}
|
wsLongConn = newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
|
||||||
compressProtoc, exists = connContext.GetHeader(Compression)
|
if err := wsLongConn.GenerateLongConn(w, r); err != nil {
|
||||||
if exists {
|
httpError(connContext, err)
|
||||||
if compressProtoc == GzipCompressionProtocol {
|
return
|
||||||
compression = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client := ws.clientPool.Get().(*Client)
|
client := ws.clientPool.Get().(*Client)
|
||||||
client.ResetClient(connContext, wsLongConn, connContext.GetBackground(), compression, ws, token)
|
client.ResetClient(connContext, wsLongConn, connContext.GetBackground(), args.Compression, ws, args.Token)
|
||||||
ws.registerChan <- client
|
ws.registerChan <- client
|
||||||
go client.readMessage()
|
go client.readMessage()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user