From 76f95a422b56a223e331c691ebbd3dad3b0574ac Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Tue, 20 Dec 2022 20:30:58 -0500 Subject: [PATCH] newdata can switch on command --- pkg/blas/core/core.go | 7 +++++-- pkg/frontend/frontend.go | 12 +++++++++++- pkg/wsapi/api.go | 13 ++++++++++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/pkg/blas/core/core.go b/pkg/blas/core/core.go index 416f7dd..5784368 100644 --- a/pkg/blas/core/core.go +++ b/pkg/blas/core/core.go @@ -8,6 +8,8 @@ import ( "dynatron.me/x/blasphem/pkg/components" "dynatron.me/x/blasphem/pkg/config" "dynatron.me/x/blasphem/pkg/storage" + + "github.com/gorilla/websocket" ) type Blas interface { @@ -32,12 +34,13 @@ type WebSocketManager interface { } type WebSocketSession interface { + WSConn() *websocket.Conn Go() error Blas() Blas } -type Handler func(wss WebSocketSession, msgID int, msg interface{}) error -type NewData func() interface{} +type Handler func(wss WebSocketSession, msgID int, cmd []string, msg interface{}) error +type NewData func(cmd []string) interface{} type Shutdowner interface { ShutdownBlas(context.Context) error diff --git a/pkg/frontend/frontend.go b/pkg/frontend/frontend.go index 9d02411..47cc2ac 100644 --- a/pkg/frontend/frontend.go +++ b/pkg/frontend/frontend.go @@ -52,7 +52,15 @@ func (fe *Frontend) AliasHandler(toFile string) echo.HandlerFunc { func (*Frontend) Shutdown() {} -func Setup(_ core.Blas) (components.Component, error) { +func newData(_ []string) interface{} { + return map[string]interface{}{} +} + +func wsHand(wss core.WebSocketSession, msgID int, cmd []string, msg interface{}) error { + return nil +} + +func Setup(b core.Blas) (components.Component, error) { fe := &Frontend{} var err error @@ -63,6 +71,8 @@ func Setup(_ core.Blas) (components.Component, error) { fe.fsHandler = echo.StaticDirectoryHandler(fe.rootFS, false) + b.RegisterWSCommand("frontend", wsHand, newData) + return fe, nil } diff --git a/pkg/wsapi/api.go b/pkg/wsapi/api.go index 61e5f75..454f807 100644 --- a/pkg/wsapi/api.go +++ b/pkg/wsapi/api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "io" + "strings" "dynatron.me/x/blasphem/pkg/auth" "dynatron.me/x/blasphem/pkg/blas/core" @@ -88,6 +89,10 @@ func NewSession(s core.Blas, c echo.Context, conn *websocket.Conn) core.WebSocke return ws } +func (ws *wsSession) WSConn() *websocket.Conn { + return ws.Conn +} + func (ws *wsSession) Blas() core.Blas { return ws.b } func (ws *wsSession) Go() error { @@ -166,7 +171,9 @@ func (ws *cmdHandler) handleMsg(r io.Reader) error { id := int(idFl) - newData, hand, err := ws.b.WSCommandHandler(msgType) + cmd := strings.Split(msgType, "/") + + newData, hand, err := ws.b.WSCommandHandler(cmd[0]) switch err { case nil: case NoSuchHandlerErr: @@ -178,6 +185,6 @@ func (ws *cmdHandler) handleMsg(r io.Reader) error { return err } - nd := newData() - return hand(ws, id, nd) + nd := newData(cmd) + return hand(ws, id, cmd, nd) }