MCPcopy
hub / github.com/openimsdk/open-im-server / wsHandler

Method wsHandler

internal/msggateway/ws_server.go:485–543  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

483}
484
485func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
486 // Create a new connection context
487 connContext := newContext(w, r)
488
489 // Check if the current number of online user connections exceeds the maximum limit
490 if ws.onlineUserConnNum.Load() >= ws.wsMaxConnNum {
491 // If it exceeds the maximum connection number, return an error via HTTP and stop processing
492 ws.handlerError(connContext, w, r, servererrs.ErrConnOverMaxNumLimit.WrapMsg("over max conn num limit"))
493 return
494 }
495
496 // Parse essential arguments (e.g., user ID, Token)
497 err := connContext.ParseEssentialArgs()
498 if err != nil {
499 // If there's an error during parsing, return an error via HTTP and stop processing
500 ws.handlerError(connContext, w, r, err)
501 return
502 }
503
504 // Call the authentication client to parse the Token obtained from the context
505 resp, err := ws.authClient.ParseToken(connContext, connContext.GetToken())
506 if err != nil {
507 ws.handlerError(connContext, w, r, err)
508 return
509 }
510
511 // Validate the authentication response matches the request (e.g., user ID and platform ID)
512 err = ws.validateRespWithRequest(connContext, resp)
513 if err != nil {
514 // If validation fails, return an error via HTTP and stop processing
515 ws.handlerError(connContext, w, r, err)
516 return
517 }
518 conn, err := ws.websocket.Upgrade(w, r, nil)
519 if err != nil {
520 log.ZWarn(connContext, "websocket upgrade failed", err)
521 return
522 }
523 if connContext.ShouldSendResp() {
524 if err := conn.WriteMessage(websocket.TextMessage, wsSuccessResponse); err != nil {
525 log.ZWarn(connContext, "WriteMessage first response", err)
526 return
527 }
528 }
529 log.ZDebug(connContext, "new conn", "token", connContext.GetToken())
530
531 var pingInterval time.Duration
532 if connContext.GetPlatformID() == constant.WebPlatformID {
533 pingInterval = pingPeriod
534 }
535
536 // Retrieve a client object from the client pool, reset its state, and associate it with the current WebSocket long connection
537 client := ws.clientPool.Get().(*Client)
538 client.ResetClient(connContext, NewWebSocketClientConn(conn, maxMessageSize, pongWait, pingInterval), ws)
539
540 // Register the client with the server and start message processing
541 ws.registerChan <- client
542 go client.readMessage()

Callers

nothing calls this directly

Calls 13

handlerErrorMethod · 0.95
newContextFunction · 0.85
NewWebSocketClientConnFunction · 0.85
ParseEssentialArgsMethod · 0.80
GetTokenMethod · 0.80
ShouldSendRespMethod · 0.80
GetPlatformIDMethod · 0.80
ResetClientMethod · 0.80
WriteMessageMethod · 0.65
GetMethod · 0.65
ParseTokenMethod · 0.45

Tested by

no test coverage detected