How Can I Get Websocket Timeout Err Message Or Event
- Dominant language
- Go
- Stars
- 25.6k
- Forks
- 2.4k
- PR merge metrics
- No merged PRs in 30d
Description
Hey Bro, Thank you for your project.
Is there a way to get errors? When the client disconnects, let me know whether it is a timeout disconnection or something else
```go
ws := websocket.New(gorilla.Upgrader(
gorillaWs.Upgrader{CheckOrigin: func(*http.Request) bool { return true }},
), websocket.Events{
websocket.OnNativeMessage: func(nsConn *websocket.NSConn, msg websocket.Message) error {
Listen.WS = nsConn.Conn.Server()
Listen.Ctx = websocket.GetContext(nsConn.Conn) //todo
Listen.Get(msg, nsConn.Conn.ID())
nsConn.Conn.Socket().NetConn().SetReadDeadline(time.Now().Add(1 * time.Minute))
return nil
},
//websocket.OnAnyEvent 不可用
})
ws.FireDisconnectAlways = true
ws.OnUpgradeError = func(err error) {
util.GetWsLog().Infof("设备连接失败 [%s]", err.Error())
}
ws.OnConnect = func(c *websocket.Conn) error {
util.GetWsLog().Infof("Websocket连接 [%s]", c.ID())
return nil
}
ws.OnDisconnect = Listen.onLeave
util.GetWsLog().Infof("websocketController:%p", Listen)
go Listen.RedisReadMsg()
app.Get("/ws", websocket.Handler(ws))
```
Contributor guide
Research direction
Start with the websocket.Events configuration and the OnDisconnect callback shown in the issue, then inspect how NetConn().SetReadDeadline interacts with disconnect handling. Done means documenting or exposing whether a client disconnect was caused by a timeout or another error, with the behavior covered for the shown websocket setup.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- networking
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100