works:programmer:go:error-casting
Преобразование Error в исходный тип websocket.CloseError
На примере Gorilla Websocket CloseGoingAway
r.GET("/ws", func(c *gin.Context) { ws, err := socketUpgrade.Upgrade(c.Writer, c.Request, nil) defer ws.Close() var data struct { A string `json:"a"` B int `json:"b"` } count := 0 for { err = ws.ReadJSON(&data) if err != nil { if closeErr, ok := err.(*websocket.CloseError); ok && closeErr.Code == websocket.CloseGoingAway { log.Println("ws_client disconnected") return } assert(err) } count += 1 err = ws.WriteJSON(gin.H{ "a": data.A, "b": data.B, "c": count, }) if err != nil { if closeErr, ok := err.(*websocket.CloseError); ok && closeErr.Code == websocket.CloseGoingAway { log.Println("ws_client disconnected") return } assert(err) } } })
works/programmer/go/error-casting.txt · Последнее изменение: 2021/08/01 02:58 — 127.0.0.1