From 5570e53d997334b225fd8941c718d67b17797795 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Tue, 20 Dec 2022 21:22:00 -0500 Subject: [PATCH] handle close --- pkg/wsapi/api.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/wsapi/api.go b/pkg/wsapi/api.go index 02be019..5ecd3f8 100644 --- a/pkg/wsapi/api.go +++ b/pkg/wsapi/api.go @@ -11,6 +11,7 @@ import ( "github.com/gorilla/websocket" "github.com/labstack/echo/v4" + "github.com/mitchellh/mapstructure" "github.com/rs/zerolog/log" ) @@ -106,14 +107,18 @@ func (ws *wsSession) Go() error { for { _, rdr, err := ws.NextReader() - if err != nil { + switch { + case websocket.IsCloseError(err, websocket.CloseGoingAway): + return nil + case err == nil: + case err != nil: log.Error().Err(err).Str("remote", ws.ec.Request().RemoteAddr).Msg("websocket read fail") return err } err = ws.h.handleMsg(rdr) if err != nil { - return err + log.Error().Err(err).Msg("handleMsg") } } } @@ -190,5 +195,15 @@ func (ws *cmdHandler) handleMsg(r io.Reader) error { } nd := newData(cmd) + err = mapstructure.Decode(&msgMap, &nd) + if err != nil { + ws.writeError(id, Error{ + Code: "invalid_format", + Message: err.Error(), + }) + + return nil + } + return hand(ws, id, cmd, nd) }