mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
use websocket message.
This commit is contained in:
parent
47d0359106
commit
c82a083fb6
@ -6,11 +6,6 @@ import (
|
||||
"demodesk/neko/internal/types"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Event string `json:"event"`
|
||||
Payload interface{} `json:"payload"` // TODO: New.
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// System
|
||||
/////////////////////////////
|
||||
|
@ -1,8 +1,16 @@
|
||||
package types
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HandlerFunction func(Session, []byte) bool
|
||||
type WebSocketMessage struct {
|
||||
Event string `json:"event"`
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
}
|
||||
|
||||
type HandlerFunction func(Session, WebSocketMessage) bool
|
||||
|
||||
type CheckOrigin func(r *http.Request) bool
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
@ -35,38 +33,35 @@ type MessageHandlerCtx struct {
|
||||
capture types.CaptureManager
|
||||
}
|
||||
|
||||
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) bool {
|
||||
logger := h.logger.With().Str("session_id", session.ID()).Logger()
|
||||
|
||||
header := message.Message{}
|
||||
if err := json.Unmarshal(raw, &header); err != nil {
|
||||
logger.Error().Err(err).Msg("message parsing has failed")
|
||||
return false
|
||||
}
|
||||
func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketMessage) bool {
|
||||
logger := h.logger.With().
|
||||
Str("event", data.Event).
|
||||
Str("session_id", session.ID()).
|
||||
Logger()
|
||||
|
||||
var err error
|
||||
switch header.Event {
|
||||
switch data.Event {
|
||||
// Signal Events
|
||||
case event.SIGNAL_REQUEST:
|
||||
payload := &message.SignalVideo{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.signalRequest(session, payload)
|
||||
})
|
||||
case event.SIGNAL_RESTART:
|
||||
err = h.signalRestart(session)
|
||||
case event.SIGNAL_ANSWER:
|
||||
payload := &message.SignalAnswer{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.signalAnswer(session, payload)
|
||||
})
|
||||
case event.SIGNAL_CANDIDATE:
|
||||
payload := &message.SignalCandidate{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.signalCandidate(session, payload)
|
||||
})
|
||||
case event.SIGNAL_VIDEO:
|
||||
payload := &message.SignalVideo{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.signalVideo(session, payload)
|
||||
})
|
||||
|
||||
@ -79,38 +74,38 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) bool {
|
||||
// Screen Events
|
||||
case event.SCREEN_SET:
|
||||
payload := &message.ScreenSize{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.screenSet(session, payload)
|
||||
})
|
||||
|
||||
// Clipboard Events
|
||||
case event.CLIPBOARD_SET:
|
||||
payload := &message.ClipboardData{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.clipboardSet(session, payload)
|
||||
})
|
||||
|
||||
// Keyboard Events
|
||||
case event.KEYBOARD_MAP:
|
||||
payload := &message.KeyboardMap{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.keyboardMap(session, payload)
|
||||
})
|
||||
case event.KEYBOARD_MODIFIERS:
|
||||
payload := &message.KeyboardModifiers{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.keyboardModifiers(session, payload)
|
||||
})
|
||||
|
||||
// Send Events
|
||||
case event.SEND_UNICAST:
|
||||
payload := &message.SendUnicast{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.sendUnicast(session, payload)
|
||||
})
|
||||
case event.SEND_BROADCAST:
|
||||
payload := &message.SendBroadcast{}
|
||||
err = utils.Unmarshal(payload, raw, func() error {
|
||||
err = utils.Unmarshal(payload, data.Payload, func() error {
|
||||
return h.sendBroadcast(session, payload)
|
||||
})
|
||||
default:
|
||||
@ -118,7 +113,7 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) bool {
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.Error().Err(err).Str("event", header.Event).Msg("message handler has failed")
|
||||
logger.Error().Err(err).Msg("message handler has failed")
|
||||
}
|
||||
|
||||
return true
|
||||
|
@ -1,6 +1,7 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@ -296,22 +297,32 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, session t
|
||||
for {
|
||||
select {
|
||||
case raw := <-bytes:
|
||||
data := types.WebSocketMessage{}
|
||||
if err := json.Unmarshal(raw, &data); err != nil {
|
||||
logger.Error().Err(err).Msg("message parsing has failed")
|
||||
break
|
||||
}
|
||||
|
||||
// TODO: Switch to payload based messages.
|
||||
data.Payload = raw
|
||||
|
||||
logger.Debug().
|
||||
Str("address", connection.RemoteAddr().String()).
|
||||
Str("raw", string(raw)).
|
||||
Str("event", data.Event).
|
||||
Str("payload", string(data.Payload)).
|
||||
Msg("received message from client")
|
||||
|
||||
handled := manager.handler.Message(session, raw)
|
||||
handled := manager.handler.Message(session, data)
|
||||
for _, handler := range manager.handlers {
|
||||
if handled {
|
||||
break
|
||||
}
|
||||
|
||||
handled = handler(session, raw)
|
||||
handled = handler(session, data)
|
||||
}
|
||||
|
||||
if !handled {
|
||||
logger.Warn().Msg("unhandled message")
|
||||
logger.Warn().Str("event", data.Event).Msg("unhandled message")
|
||||
}
|
||||
case <-cancel:
|
||||
return
|
||||
|
Loading…
Reference in New Issue
Block a user