neko/internal/websocket/handler/system.go
2021-09-09 22:08:39 +02:00

99 lines
2.1 KiB
Go

package handler
import (
"errors"
"github.com/rs/zerolog"
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
func (h *MessageHandlerCtx) systemInit(session types.Session) error {
host := h.sessions.GetHost()
controlHost := message.ControlHost{
HasHost: host != nil,
}
if controlHost.HasHost {
controlHost.HostID = host.ID()
}
size := h.desktop.GetScreenSize()
if size == nil {
return errors.New("could not get screen size")
}
sessions := map[string]message.SessionData{}
for _, session := range h.sessions.List() {
sessionId := session.ID()
sessions[sessionId] = message.SessionData{
ID: sessionId,
Profile: session.Profile(),
State: session.State(),
}
}
session.Send(
event.SYSTEM_INIT,
message.SystemInit{
SessionId: session.ID(),
ControlHost: controlHost,
ScreenSize: message.ScreenSize{
Width: size.Width,
Height: size.Height,
Rate: size.Rate,
},
Sessions: sessions,
ImplicitHosting: h.sessions.ImplicitHosting(),
ScreencastEnabled: h.capture.Screencast().Enabled(),
WebRTC: message.SystemWebRTC{
Videos: h.capture.VideoIDs(),
},
})
return nil
}
func (h *MessageHandlerCtx) systemAdmin(session types.Session) error {
screenSizesList := []message.ScreenSize{}
for _, size := range h.desktop.ScreenConfigurations() {
for _, rate := range size.Rates {
screenSizesList = append(screenSizesList, message.ScreenSize{
Width: size.Width,
Height: size.Height,
Rate: rate,
})
}
}
broadcast := h.capture.Broadcast()
session.Send(
event.SYSTEM_ADMIN,
message.SystemAdmin{
ScreenSizesList: screenSizesList,
BroadcastStatus: message.BroadcastStatus{
IsActive: broadcast.Started(),
URL: broadcast.Url(),
},
})
return nil
}
func (h *MessageHandlerCtx) systemLogs(session types.Session, payload *message.SystemLogs) error {
for _, log := range *payload {
level, _ := zerolog.ParseLevel(log.Level)
h.logger.WithLevel(level).
Fields(log.Fields).
Str("session_id", session.ID()).
Str("service", "frontend").
Msg(log.Message)
}
return nil
}