blasphem/pkg/wsapi/api.go
2022-12-20 13:16:30 -05:00

80 lines
1.1 KiB
Go

package wsapi
import (
"io"
"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(io.Reader) error
}
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 {
_, rdr, err := ws.NextReader()
if 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
}
}
}
type cmdMsg struct {
}
type MsgType string
func (ws *cmdHandler) handleMsg(r io.Reader) error {
return nil
}