gofiber / contrib

🧬 Repository for third party middlewares with dependencies
https://docs.gofiber.io/contrib/
MIT License
198 stars 102 forks source link

🐛 [Bug]: Websocket write tcp4 127.0.0.1:8001->127.0.0.1:59510: write: broken pipe #1111

Closed Ja7ad closed 1 month ago

Ja7ad commented 1 month ago

Bug Description

I send channel received message to ws.WriteJson, but after some connect and disconnect get error:

 write tcp4 127.0.0.1:8001->127.0.0.1:59510: write: broken pipe 

How to Reproduce

I get message from channel realtime then write in websocket

Expected Behavior

How to understand how many client connected and how handle this issue don't get this error.

Contrib package Version

v1.3.1

Code Snippet (optional)

fb.Use(cache.New(cache.Config{
            Next: func(c *fiber.Ctx) bool {
                return strings.Contains(c.Route().Path, "/ws")
            },
        }))

        fb.Use("/ws", func(c *fiber.Ctx) error {
            if websocket.IsWebSocketUpgrade(c) {
                c.Locals("allowed", true)
                return c.Next()
            }
            return fiber.ErrUpgradeRequired
        })

func wsConfig() websocket.Config {
    return websocket.Config{
        ReadBufferSize:  1024,
        WriteBufferSize: 8192,
        RecoverHandler: func(conn *websocket.Conn) {
            if err := recover(); err != nil {
                log.Println("ws recover err:", err)
            }
        },
    }
}

app.Get("ws/block", websocket.New(c.LiveBlock, wsConfig()))

func (c *Controller) LiveBlock(ws *websocket.Conn) {
    for block := range c.blockCh {
        if err := ws.WriteJSON(block); err != nil {
            continue
        }
    }
}

Checklist:

ReneWerner87 commented 1 month ago

The error write tcp4 127.0.0.1:8001->127.0.0.1:59510: write: broken pipe usually occurs when attempting to write to a closed connection. This can happen if the WebSocket connection is closed by the client or if there is an issue with network connectivity.

Ja7ad commented 1 month ago

The error write tcp4 127.0.0.1:8001->127.0.0.1:59510: write: broken pipe usually occurs when attempting to write to a closed connection. This can happen if the WebSocket connection is closed by the client or if there is an issue with network connectivity.

Sure, but after reconnecting, this error intermittently occurred while writing. I changed my code to handle the connection closure, but my problem was not resolved.

func (c *Controller) LiveBlock(ws *websocket.Conn) {
    done := make(chan bool)

    go func() {
        for {
            t, _, err := ws.ReadMessage()
            if err != nil {
                log.Println("Error reading message:", err)
                return
            }

            if t == websocket.CloseMessage {
                done <- true
            }
        }
    }()

    for {
        select {
        case <-done:
            return
        case block := <-c.blockCh:
            if err := ws.WriteJSON(block); err != nil {
                log.Println(err)
            }
        }
    }
}
Ja7ad commented 1 month ago

I found my issue and fixed it