mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
AddHandler in WebSockets.
This commit is contained in:
parent
af463b1015
commit
5b69413e01
@ -3,7 +3,6 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
@ -38,10 +37,11 @@ type MessageHandlerCtx struct {
|
|||||||
capture types.CaptureManager
|
capture types.CaptureManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
|
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) bool {
|
||||||
header := message.Message{}
|
header := message.Message{}
|
||||||
if err := json.Unmarshal(raw, &header); err != nil {
|
if err := json.Unmarshal(raw, &header); err != nil {
|
||||||
return err
|
h.logger.Error().Err(err).Msg("message parsing has failed")
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@ -87,8 +87,12 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
|
|||||||
return h.keyboardModifiers(session, payload)
|
return h.keyboardModifiers(session, payload)
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
return errors.Errorf("unknown message event %s", header.Event)
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.Wrapf(err, "%s failed", header.Event)
|
if err != nil {
|
||||||
|
h.logger.Error().Err(err).Msg("message handler has failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@ import (
|
|||||||
"demodesk/neko/internal/types"
|
"demodesk/neko/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type HandlerFunction func(types.Session, []byte) bool
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
sessions types.SessionManager,
|
sessions types.SessionManager,
|
||||||
desktop types.DesktopManager,
|
desktop types.DesktopManager,
|
||||||
@ -33,6 +35,7 @@ func New(
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: handler.New(sessions, desktop, capture, webrtc),
|
handler: handler.New(sessions, desktop, capture, webrtc),
|
||||||
|
handlers: []HandlerFunction{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,6 +48,7 @@ type WebSocketManagerCtx struct {
|
|||||||
sessions types.SessionManager
|
sessions types.SessionManager
|
||||||
desktop types.DesktopManager
|
desktop types.DesktopManager
|
||||||
handler *handler.MessageHandlerCtx
|
handler *handler.MessageHandlerCtx
|
||||||
|
handlers []HandlerFunction
|
||||||
shutdown chan bool
|
shutdown chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,6 +149,10 @@ func (ws *WebSocketManagerCtx) Shutdown() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ws *WebSocketManagerCtx) AddHandler(handler HandlerFunction) {
|
||||||
|
ws.handlers = append(ws.handlers, handler)
|
||||||
|
}
|
||||||
|
|
||||||
func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) error {
|
func (ws *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request) error {
|
||||||
ws.logger.Debug().Msg("attempting to upgrade connection")
|
ws.logger.Debug().Msg("attempting to upgrade connection")
|
||||||
|
|
||||||
@ -260,8 +268,17 @@ func (ws *WebSocketManagerCtx) handle(connection *websocket.Conn, session types.
|
|||||||
Str("raw", string(raw)).
|
Str("raw", string(raw)).
|
||||||
Msg("received message from client")
|
Msg("received message from client")
|
||||||
|
|
||||||
if err := ws.handler.Message(session, raw); err != nil {
|
handled := ws.handler.Message(session, raw)
|
||||||
ws.logger.Error().Err(err).Msg("message handler has failed")
|
for _, handler := range ws.handlers {
|
||||||
|
if handled {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
handled = handler(session, raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !handled {
|
||||||
|
ws.logger.Warn().Msg("unhandled message")
|
||||||
}
|
}
|
||||||
case <-cancel:
|
case <-cancel:
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user