refactor: websocket update info.

pull/2148/head
Gordon 1 year ago
parent 2a429d6163
commit 580bd5cc93

@ -26,7 +26,7 @@ const (
Compression = "compression"
GzipCompressionProtocol = "gzip"
BackgroundStatus = "isBackground"
ErrResp = "errResp"
SendResponse = "isMsgResp"
)
const (

@ -149,8 +149,8 @@ func (c *UserConnContext) GetCompression() bool {
return false
}
func (c *UserConnContext) ShouldSendError() bool {
errResp, exists := c.Query(ErrResp)
func (c *UserConnContext) ShouldSendResp() bool {
errResp, exists := c.Query(SendResponse)
if exists {
b, err := strconv.ParseBool(errResp)
if err != nil {

@ -14,8 +14,12 @@
package msggateway
import "github.com/openimsdk/tools/apiresp"
import (
"github.com/openimsdk/tools/apiresp"
"github.com/openimsdk/tools/log"
)
func httpError(ctx *UserConnContext, err error) {
log.ZWarn(ctx, "ws connection error", err)
apiresp.HttpError(ctx.RespWriter, err)
}

@ -145,7 +145,7 @@ func (d *GWebSocket) SetPingHandler(handler PingPongHandler) {
d.conn.SetPingHandler(handler)
}
func (d *GWebSocket) RespErrInfo(err error, w http.ResponseWriter, r *http.Request) error {
func (d *GWebSocket) RespondWithError(err error, w http.ResponseWriter, r *http.Request) error {
if err := d.GenerateLongConn(w, r); err != nil {
return err
}
@ -163,6 +163,16 @@ func (d *GWebSocket) RespErrInfo(err error, w http.ResponseWriter, r *http.Reque
return nil
}
// func (d *GWebSocket) CheckSendConnDiffNow() bool {
// return d.conn == d.sendConn
//}
func (d *GWebSocket) RespondWithSuccess() error {
data, err := json.Marshal(apiresp.ParseError(nil))
if err != nil {
_ = d.Close()
return errs.WrapMsg(err, "json marshal failed")
}
if err := d.WriteMessage(MessageText, data); err != nil {
_ = d.Close()
return errs.WrapMsg(err, "WriteMessage failed")
}
return nil
}

@ -391,11 +391,11 @@ func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
resp, err := ws.authClient.ParseToken(connContext, connContext.GetToken())
if err != nil {
// If there's an error parsing the Token, decide whether to send the error message via WebSocket based on the context flag
shouldSendError := connContext.ShouldSendError()
shouldSendError := connContext.ShouldSendResp()
if shouldSendError {
// Create a WebSocket connection object and attempt to send the error message via WebSocket
wsLongConn := newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
if err := wsLongConn.RespErrInfo(err, w, r); err == nil {
if err := wsLongConn.RespondWithError(err, w, r); err == nil {
// If the error message is successfully sent via WebSocket, stop processing
return
}
@ -419,6 +419,16 @@ func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
// If creating the long connection fails, return an error via HTTP and stop processing
httpError(connContext, err)
return
} else {
// Check if a normal response should be sent via WebSocket
shouldSendSuccessResp := connContext.ShouldSendResp()
if shouldSendSuccessResp {
// Attempt to send a success message through WebSocket
if err := wsLongConn.RespondWithSuccess(); err == nil {
// If the success message is successfully sent, end further processing
return
}
}
}
// Retrieve a client object from the client pool, reset its state, and associate it with the current WebSocket long connection

Loading…
Cancel
Save