mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-11-06 21:32:15 +08:00
refactor: websocket update info.
This commit is contained in:
parent
2a429d6163
commit
580bd5cc93
@ -26,7 +26,7 @@ const (
|
|||||||
Compression = "compression"
|
Compression = "compression"
|
||||||
GzipCompressionProtocol = "gzip"
|
GzipCompressionProtocol = "gzip"
|
||||||
BackgroundStatus = "isBackground"
|
BackgroundStatus = "isBackground"
|
||||||
ErrResp = "errResp"
|
SendResponse = "isMsgResp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@ -149,8 +149,8 @@ func (c *UserConnContext) GetCompression() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *UserConnContext) ShouldSendError() bool {
|
func (c *UserConnContext) ShouldSendResp() bool {
|
||||||
errResp, exists := c.Query(ErrResp)
|
errResp, exists := c.Query(SendResponse)
|
||||||
if exists {
|
if exists {
|
||||||
b, err := strconv.ParseBool(errResp)
|
b, err := strconv.ParseBool(errResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -14,8 +14,12 @@
|
|||||||
|
|
||||||
package msggateway
|
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) {
|
func httpError(ctx *UserConnContext, err error) {
|
||||||
|
log.ZWarn(ctx, "ws connection error", err)
|
||||||
apiresp.HttpError(ctx.RespWriter, err)
|
apiresp.HttpError(ctx.RespWriter, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -145,7 +145,7 @@ func (d *GWebSocket) SetPingHandler(handler PingPongHandler) {
|
|||||||
d.conn.SetPingHandler(handler)
|
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 {
|
if err := d.GenerateLongConn(w, r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -163,6 +163,16 @@ func (d *GWebSocket) RespErrInfo(err error, w http.ResponseWriter, r *http.Reque
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (d *GWebSocket) CheckSendConnDiffNow() bool {
|
func (d *GWebSocket) RespondWithSuccess() error {
|
||||||
// return d.conn == d.sendConn
|
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())
|
resp, err := ws.authClient.ParseToken(connContext, connContext.GetToken())
|
||||||
if err != nil {
|
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
|
// 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 {
|
if shouldSendError {
|
||||||
// Create a WebSocket connection object and attempt to send the error message via WebSocket
|
// Create a WebSocket connection object and attempt to send the error message via WebSocket
|
||||||
wsLongConn := newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
|
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
|
// If the error message is successfully sent via WebSocket, stop processing
|
||||||
return
|
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
|
// If creating the long connection fails, return an error via HTTP and stop processing
|
||||||
httpError(connContext, err)
|
httpError(connContext, err)
|
||||||
return
|
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
|
// Retrieve a client object from the client pool, reset its state, and associate it with the current WebSocket long connection
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user