- With issues:
- Use the search tool before opening a new issue.
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.
Description
I used gorilla/websocket combined with the gin framework, and an error occurred when connecting to websocket, indicating that the connection was hijacked.
How to reproduce
router.GET("/wss/push", pushMessage)
router.Run(":8080")
func pushMessage(c *gin.Context) {
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
log.Fatalf("upgrade http to websocket fail:%s", err)
return
}
defer func(ws *websocket.Conn) {
err := ws.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
log.Fatalf("close websocket fail:%s", err)
}
}(ws)
go func() {
for {
select {
case msg, ok := <-receiveMsg:
if ok {
jsonData, err := json.Marshal(msg)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
log.Printf("recevie message convert to json fail:%s", err)
}
err = ws.WriteMessage(websocket.TextMessage, jsonData)
if err != nil {
c.JSON(http.StatusInternalServerError, BaseResponse{Code: CODE_INTERNAL_ERROR, Message: ""})
log.Printf("websocket write message fail:%s", err)
}
}
}
}
}()
c.JSON(http.StatusOK, BaseResponse{Code: CODE_SUCCESS, Message: "success"})
}
Expectations
Connect to websocket normally
Actual result
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with 500
2024/04/11 19:07:59 http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83)
[GIN] 2024/04/11 - 19:07:59 | 200 | 17.8844ms | 127.0.0.1 | GET "/wss/push"
Error #01: http: connection has been hijacked
Error #02: http: connection has been hijacked
Environment
- go version: 1.22.2
- gin version (or commit ref):1.9.1
- operating system:windows 11
Comment From: RedCrazyGhost
The code you provided cannot be reproduced, please provide the code again