blasphem/pkg/wsapi/api.go

82 lines
1.1 KiB
Go
Raw Normal View History

2022-12-19 19:24:01 -05:00
package wsapi
import (
2022-12-20 11:34:25 -05:00
"io"
2022-12-19 19:24:01 -05:00
"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 {
2022-12-20 11:34:25 -05:00
handleMsg(io.Reader) error
2022-12-19 19:24:01 -05:00
}
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 {
2022-12-20 11:34:25 -05:00
_, rdr, err := ws.NextReader()
2022-12-19 19:24:01 -05:00
if err != nil {
log.Error().Err(err).Str("remote", ws.ec.Request().RemoteAddr).Msg("websocket read fail")
2022-12-20 11:34:25 -05:00
return err
2022-12-19 19:24:01 -05:00
}
2022-12-20 11:34:25 -05:00
err = ws.h.handleMsg(rdr)
2022-12-19 19:24:01 -05:00
if err != nil {
return err
}
}
}
type cmdMsg struct {
}
type MsgType string
2022-12-20 11:34:25 -05:00
func (ws *cmdHandler) handleMsg(r io.Reader) error {
2022-12-19 19:24:01 -05:00
return nil
}