package wsapi import ( "encoding/json" "dynatron.me/x/blasphem/pkg/auth" "dynatron.me/x/blasphem/pkg/blas" "github.com/gorilla/websocket" "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" ) type Type string type MsgBase struct { Type Type `json:"type"` } type ( wsSession struct { *websocket.Conn b blas.Core ec echo.Context h phaseHandler user *auth.User refreshToken *auth.RefreshToken } WS interface { Handle() error } phaseHandler interface { handleMsg(msg interface{}) error msgSchema() interface{} } cmdHandler struct { *wsSession } ) func New(s blas.Core, c echo.Context, conn *websocket.Conn) WS { ws := &wsSession{ Conn: conn, b: s, ec: c, } return ws } func (ws *wsSession) Handle() error { err := ws.sendAuthRequired() if err != nil { return err } for { msg := ws.h.msgSchema() err := ws.ReadJSON(msg) if err != nil { log.Error().Err(err).Str("remote", ws.ec.Request().RemoteAddr).Msg("websocket read fail") } err = ws.h.handleMsg(msg) if err != nil { return err } } } type cmdMsg struct { } type MsgType string func (cm *cmdMsg) UnmarshalJSON(b []byte) error { var t typeType struct { Type MsgType `json:"type"` } err := json.Unmarshal(b, &t) if err != nil { return err } } func (ws *cmdHandler) msgSchema() interface{} { return &cmdMsg{} } func (ws *cmdHandler) handleMsg(msg interface{}) error { return nil }