gofiber / websocket

⚠️ Deprecated repository, available within Fiber Contrib.
https://github.com/gofiber/contrib/tree/main/websocket
MIT License
288 stars 25 forks source link

connect close fail without error #81

Open luyouxin84 opened 2 years ago

luyouxin84 commented 2 years ago

Fiber version "github.com/gofiber/fiber/v2" "github.com/gofiber/websocket/v2"

Issue description c := *websocket.Conn c.Close() do not close connect and has not error return

Code snippet

app.Get("/ws", websocket.New(func(c *websocket.Conn) {
        // When the function returns, unregister the client and close the connection
        defer func() {
            unregister <- c
            c.Close()
        }()
        register <- c
        for {
            messageType, message, err := c.ReadMessage()
            if err != nil {
                if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
                    log.Println("read error:", err)
                }

                return // Calls the deferred function, i.e. closes the connection on error
            }

            if messageType == websocket.TextMessage {
                // Broadcast the received message
                broadcast <- string(message)
            } else {
                log.Println("websocket message received of type", messageType)
            }
        }
    }))

this is code from your example: https://github.com/gofiber/recipes/blob/master/websocket-chat/main.go.. u can call c.Close() immediately but websocket still alive

ReneWerner87 commented 2 years ago

initial post in the wrong repository https://github.com/gofiber/fiber/issues/1668

outcome in https://github.com/gofiber/fiber/issues/1668#issuecomment-1002111084