remove event from messages.

This commit is contained in:
Miroslav Šedivý 2021-09-01 21:58:39 +02:00
parent 3ac1b481ad
commit 49c48b3200
12 changed files with 144 additions and 173 deletions

View File

@ -44,8 +44,8 @@ func (h *RoomHandler) boradcastStart(w http.ResponseWriter, r *http.Request) {
}
h.sessions.AdminBroadcast(
event.BORADCAST_STATUS,
message.BroadcastStatus{
Event: event.BORADCAST_STATUS,
IsActive: broadcast.Started(),
URL: broadcast.Url(),
}, nil)
@ -63,8 +63,8 @@ func (h *RoomHandler) boradcastStop(w http.ResponseWriter, r *http.Request) {
broadcast.Stop()
h.sessions.AdminBroadcast(
event.BORADCAST_STATUS,
message.BroadcastStatus{
Event: event.BORADCAST_STATUS,
IsActive: broadcast.Started(),
URL: broadcast.Url(),
}, nil)

View File

@ -47,8 +47,8 @@ func (h *RoomHandler) screenConfigurationChange(w http.ResponseWriter, r *http.R
}
h.sessions.Broadcast(
event.SCREEN_UPDATED,
message.ScreenSize{
Event: event.SCREEN_UPDATED,
Width: data.Width,
Height: data.Height,
Rate: data.Rate,

View File

@ -11,12 +11,10 @@ import (
/////////////////////////////
type SystemWebRTC struct {
Event string `json:"event,omitempty"`
Videos []string `json:"videos"`
}
type SystemInit struct {
Event string `json:"event,omitempty"`
SessionId string `json:"session_id"`
ControlHost ControlHost `json:"control_host"`
ScreenSize ScreenSize `json:"screen_size"`
@ -27,13 +25,11 @@ type SystemInit struct {
}
type SystemAdmin struct {
Event string `json:"event,omitempty"`
ScreenSizesList []ScreenSize `json:"screen_sizes_list"`
BroadcastStatus BroadcastStatus `json:"broadcast_status"`
}
type SystemDisconnect struct {
Event string `json:"event,omitempty"`
Message string `json:"message"`
}
@ -42,24 +38,20 @@ type SystemDisconnect struct {
/////////////////////////////
type SignalProvide struct {
Event string `json:"event,omitempty"`
SDP string `json:"sdp"`
ICEServers []types.ICEServer `json:"iceservers"`
Video string `json:"video"`
}
type SignalCandidate struct {
Event string `json:"event,omitempty"`
*webrtc.ICECandidateInit
webrtc.ICECandidateInit
}
type SignalAnswer struct {
Event string `json:"event,omitempty"`
SDP string `json:"sdp"`
}
type SignalVideo struct {
Event string `json:"event,omitempty"`
Video string `json:"video"`
}
@ -68,24 +60,20 @@ type SignalVideo struct {
/////////////////////////////
type SessionID struct {
Event string `json:"event,omitempty"`
ID string `json:"id"`
}
type MemberProfile struct {
Event string `json:"event,omitempty"`
ID string `json:"id"`
*types.MemberProfile
types.MemberProfile
}
type SessionState struct {
Event string `json:"event,omitempty"`
ID string `json:"id"`
*types.SessionState
types.SessionState
}
type SessionData struct {
Event string `json:"event,omitempty"`
ID string `json:"id"`
Profile types.MemberProfile `json:"profile"`
State types.SessionState `json:"state"`
@ -96,28 +84,24 @@ type SessionData struct {
/////////////////////////////
type ControlHost struct {
Event string `json:"event,omitempty"`
HasHost bool `json:"has_host"`
HostID string `json:"host_id,omitempty"`
}
// TODO: New.
type ControlMove struct {
Event string `json:"event,omitempty"`
X uint16 `json:"x"`
Y uint16 `json:"y"`
}
// TODO: New.
type ControlScroll struct {
Event string `json:"event,omitempty"`
X int16 `json:"x"`
Y int16 `json:"y"`
}
// TODO: New.
type ControlKey struct {
Event string `json:"event,omitempty"`
Key uint32 `json:"key"`
}
@ -126,7 +110,6 @@ type ControlKey struct {
/////////////////////////////
type ScreenSize struct {
Event string `json:"event,omitempty"`
Width int `json:"width"`
Height int `json:"height"`
Rate int16 `json:"rate"`
@ -137,7 +120,6 @@ type ScreenSize struct {
/////////////////////////////
type ClipboardData struct {
Event string `json:"event,omitempty"`
Text string `json:"text"`
}
@ -146,13 +128,11 @@ type ClipboardData struct {
/////////////////////////////
type KeyboardMap struct {
Event string `json:"event,omitempty"`
Layout string `json:"layout"`
Variant string `json:"variant"`
}
type KeyboardModifiers struct {
Event string `json:"event,omitempty"`
CapsLock *bool `json:"capslock"`
NumLock *bool `json:"numlock"`
}
@ -162,7 +142,6 @@ type KeyboardModifiers struct {
/////////////////////////////
type BroadcastStatus struct {
Event string `json:"event,omitempty"`
IsActive bool `json:"is_active"`
URL string `json:"url,omitempty"`
}
@ -172,7 +151,6 @@ type BroadcastStatus struct {
/////////////////////////////
type SendUnicast struct {
Event string `json:"event,omitempty"`
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Subject string `json:"subject"`
@ -180,7 +158,6 @@ type SendUnicast struct {
}
type SendBroadcast struct {
Event string `json:"event,omitempty"`
Sender string `json:"sender"`
Subject string `json:"subject"`
Body interface{} `json:"body"`

View File

@ -141,16 +141,11 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
return
}
ICECandidateInit := candidate.ToJSON()
err := session.Send(
session.Send(
event.SIGNAL_CANDIDATE,
message.SignalCandidate{
Event: event.SIGNAL_CANDIDATE,
ICECandidateInit: &ICECandidateInit,
ICECandidateInit: candidate.ToJSON(),
})
if err != nil {
logger.Warn().Err(err).Msg("sending ice candidate failed")
}
})
}

View File

@ -22,8 +22,9 @@ func (manager *WebSocketManagerCtx) fileChooserDialogEvents() {
activeSession = host
go manager.sessions.Broadcast(message.SessionID{
Event: event.FILE_CHOOSER_DIALOG_OPENED,
go manager.sessions.Broadcast(
event.FILE_CHOOSER_DIALOG_OPENED,
message.SessionID{
ID: host.ID(),
}, nil)
})
@ -34,9 +35,9 @@ func (manager *WebSocketManagerCtx) fileChooserDialogEvents() {
activeSession = nil
go manager.sessions.Broadcast(message.SessionID{
Event: event.FILE_CHOOSER_DIALOG_CLOSED,
}, nil)
go manager.sessions.Broadcast(
event.FILE_CHOOSER_DIALOG_CLOSED,
message.SessionID{}, nil)
})
// when new user joins, and someone holds dialog, he shouldd be notified about it.
@ -45,17 +46,13 @@ func (manager *WebSocketManagerCtx) fileChooserDialogEvents() {
return
}
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
logger.Debug().Msg("sending file chooser dialog status to a new session")
manager.logger.Debug().Str("session_id", session.ID()).Msg("sending file chooser dialog status to a new session")
if err := session.Send(message.SessionID{
Event: event.FILE_CHOOSER_DIALOG_OPENED,
session.Send(
event.FILE_CHOOSER_DIALOG_OPENED,
message.SessionID{
ID: activeSession.ID(),
}); err != nil {
logger.Warn().Err(err).
Str("event", event.FILE_CHOOSER_DIALOG_OPENED).
Msg("could not send event")
}
})
})
// when user, that holds dialog, disconnects, it should be closed.

View File

@ -41,12 +41,14 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
if !h.sessions.ImplicitHosting() {
// tell session if there is a host
if host := h.sessions.GetHost(); host != nil {
return session.Send(
session.Send(
event.CONTROL_HOST,
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: host.ID(),
})
return nil
}
}

View File

@ -23,8 +23,8 @@ func (h *MessageHandlerCtx) screenSet(session types.Session, payload *message.Sc
}
h.sessions.Broadcast(
event.SCREEN_UPDATED,
message.ScreenSize{
Event: event.SCREEN_UPDATED,
Width: payload.Width,
Height: payload.Height,
Rate: payload.Rate,

View File

@ -15,18 +15,22 @@ func (h *MessageHandlerCtx) sendUnicast(session types.Session, payload *message.
return nil
}
return receiver.Send(message.SendUnicast{
Event: event.SEND_UNICAST,
receiver.Send(
event.SEND_UNICAST,
message.SendUnicast{
Sender: session.ID(),
Receiver: receiver.ID(),
Subject: payload.Subject,
Body: payload.Body,
})
return nil
}
func (h *MessageHandlerCtx) sendBroadcast(session types.Session, payload *message.SendBroadcast) error {
h.sessions.Broadcast(message.SendBroadcast{
Event: event.SEND_BROADCAST,
h.sessions.Broadcast(
event.SEND_BROADCAST,
message.SendBroadcast{
Sender: session.ID(),
Subject: payload.Subject,
Body: payload.Body,

View File

@ -8,8 +8,8 @@ import (
func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
h.sessions.Broadcast(
event.SESSION_CREATED,
message.SessionData{
Event: event.SESSION_CREATED,
ID: session.ID(),
Profile: session.Profile(),
State: session.State(),
@ -20,8 +20,8 @@ func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
func (h *MessageHandlerCtx) SessionDeleted(session types.Session) error {
h.sessions.Broadcast(
event.SESSION_DELETED,
message.SessionID{
Event: event.SESSION_DELETED,
ID: session.ID(),
}, nil)
@ -53,26 +53,22 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
}
func (h *MessageHandlerCtx) SessionProfileChanged(session types.Session) error {
profile := session.Profile()
h.sessions.Broadcast(
event.SESSION_PROFILE,
message.MemberProfile{
Event: event.SESSION_PROFILE,
ID: session.ID(),
MemberProfile: &profile,
MemberProfile: session.Profile(),
}, nil)
return nil
}
func (h *MessageHandlerCtx) SessionStateChanged(session types.Session) error {
state := session.State()
h.sessions.Broadcast(
event.SESSION_STATE,
message.SessionState{
Event: event.SESSION_STATE,
ID: session.ID(),
SessionState: &state,
SessionState: session.State(),
}, nil)
return nil

View File

@ -25,13 +25,15 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *messag
return err
}
return session.Send(
session.Send(
event.SIGNAL_PROVIDE,
message.SignalProvide{
Event: event.SIGNAL_PROVIDE,
SDP: offer.SDP,
ICEServers: h.webrtc.ICEServers(),
Video: payload.Video,
})
return nil
}
func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
@ -48,11 +50,13 @@ func (h *MessageHandlerCtx) signalRestart(session types.Session) error {
return err
}
return session.Send(
session.Send(
event.SIGNAL_RESTART,
message.SignalAnswer{
Event: event.SIGNAL_RESTART,
SDP: offer.SDP,
})
return nil
}
func (h *MessageHandlerCtx) signalAnswer(session types.Session, payload *message.SignalAnswer) error {
@ -76,7 +80,7 @@ func (h *MessageHandlerCtx) signalCandidate(session types.Session, payload *mess
return nil
}
return peer.SignalCandidate(*payload.ICECandidateInit)
return peer.SignalCandidate(payload.ICECandidateInit)
}
func (h *MessageHandlerCtx) signalVideo(session types.Session, payload *message.SignalVideo) error {
@ -93,9 +97,11 @@ func (h *MessageHandlerCtx) signalVideo(session types.Session, payload *message.
return err
}
return session.Send(
session.Send(
event.SIGNAL_VIDEO,
message.SignalVideo{
Event: event.SIGNAL_VIDEO,
Video: payload.Video,
})
return nil
}

View File

@ -33,9 +33,9 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
}
}
return session.Send(
session.Send(
event.SYSTEM_INIT,
message.SystemInit{
Event: event.SYSTEM_INIT,
SessionId: session.ID(),
ControlHost: controlHost,
ScreenSize: message.ScreenSize{
@ -50,6 +50,8 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
Videos: h.capture.VideoIDs(),
},
})
return nil
}
func (h *MessageHandlerCtx) systemAdmin(session types.Session) error {
@ -65,13 +67,15 @@ func (h *MessageHandlerCtx) systemAdmin(session types.Session) error {
}
broadcast := h.capture.Broadcast()
return session.Send(
session.Send(
event.SYSTEM_ADMIN,
message.SystemAdmin{
Event: event.SYSTEM_ADMIN,
ScreenSizesList: screenSizesList,
BroadcastStatus: message.BroadcastStatus{
IsActive: broadcast.Started(),
URL: broadcast.Url(),
},
})
return nil
}

View File

@ -105,20 +105,19 @@ func (manager *WebSocketManagerCtx) Start() {
})
manager.sessions.OnHostChanged(func(session types.Session) {
msg := message.ControlHost{
Event: event.CONTROL_HOST,
payload := message.ControlHost{
HasHost: session != nil,
}
if msg.HasHost {
msg.HostID = session.ID()
if payload.HasHost {
payload.HostID = session.ID()
}
manager.sessions.Broadcast(msg, nil)
manager.sessions.Broadcast(event.CONTROL_HOST, payload, nil)
manager.logger.Debug().
Bool("has_host", msg.HasHost).
Str("host_id", msg.HostID).
Bool("has_host", payload.HasHost).
Str("host_id", payload.HostID).
Msg("session host changed")
})
@ -128,22 +127,20 @@ func (manager *WebSocketManagerCtx) Start() {
return
}
manager.logger.Debug().Msg("sync clipboard")
data, err := manager.desktop.ClipboardGetText()
if err != nil {
manager.logger.Warn().Err(err).Msg("could not get clipboard content")
return
}
if err := session.Send(message.ClipboardData{
Event: event.CLIPBOARD_UPDATED,
session.Send(
event.CLIPBOARD_UPDATED,
message.ClipboardData{
Text: data.Text,
// TODO: Send HTML?
}); err != nil {
manager.logger.Warn().Err(err).Msg("could not sync clipboard")
return
}
manager.logger.Debug().Msg("session sync clipboard")
})
})
manager.fileChooserDialogEvents()
@ -161,13 +158,10 @@ func (manager *WebSocketManagerCtx) AddHandler(handler types.WebSocketHandler) {
}
func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Request, checkOrigin types.CheckOrigin) {
// add request data to logger context
logger := manager.logger.With().
manager.logger.Debug().
Str("address", r.RemoteAddr).
Str("agent", r.UserAgent()).
Logger()
logger.Debug().Msg("attempting to upgrade connection")
Msg("attempting to upgrade connection")
upgrader := websocket.Upgrader{
CheckOrigin: checkOrigin,
@ -175,49 +169,60 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
connection, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logger.Error().Err(err).Msg("failed to upgrade connection")
manager.logger.Error().Err(err).Msg("failed to upgrade connection")
return
}
session, err := manager.sessions.Authenticate(r)
if err != nil {
logger.Debug().Err(err).Msg("authentication failed")
manager.logger.Warn().Err(err).Msg("authentication failed")
// TODO: Refactor, return error code.
if err = connection.WriteJSON(
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
// TODO: Better handling...
raw, err := json.Marshal(message.SystemDisconnect{
Message: err.Error(),
}); err != nil {
logger.Error().Err(err).Msg("failed to send disconnect event")
})
if err != nil {
manager.logger.Error().Err(err).Msg("failed to create disconnect event")
}
err = connection.WriteJSON(
types.WebSocketMessage{
Event: event.SYSTEM_DISCONNECT,
Payload: raw,
})
if err != nil {
manager.logger.Error().Err(err).Msg("failed to send disconnect event")
}
if err := connection.Close(); err != nil {
logger.Warn().Err(err).Msg("connection closed with an error")
manager.logger.Warn().Err(err).Msg("connection closed with an error")
}
return
}
// use session id with defeault logger context
logger = manager.logger.With().Str("session_id", session.ID()).Logger()
logger := manager.logger.With().Str("session_id", session.ID()).Logger()
// create new peer
peer := &WebSocketPeerCtx{
logger: logger,
session: session,
connection: connection,
}
if !session.Profile().CanConnect {
logger.Debug().Msg("connection disabled")
logger.Warn().Msg("connection disabled")
// TODO: Refactor, return error code.
if err = connection.WriteJSON(
peer.Send(
event.SYSTEM_DISCONNECT,
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
Message: "connection disabled",
}); err != nil {
logger.Error().Err(err).Msg("failed to send disconnect event")
}
if err := connection.Close(); err != nil {
logger.Warn().Err(err).Msg("connection closed with an error")
}
})
peer.Destroy()
return
}
@ -225,32 +230,17 @@ func (manager *WebSocketManagerCtx) Upgrade(w http.ResponseWriter, r *http.Reque
logger.Warn().Msg("already connected")
if !manager.sessions.MercifulReconnect() {
// TODO: Refactor, return error code.
if err = connection.WriteJSON(
peer.Send(
event.SYSTEM_DISCONNECT,
message.SystemDisconnect{
Event: event.SYSTEM_DISCONNECT,
Message: "already connected",
}); err != nil {
logger.Error().Err(err).Msg("failed to send disconnect event")
}
if err := connection.Close(); err != nil {
logger.Warn().Err(err).Msg("connection closed with an error")
}
})
peer.Destroy()
return
}
logger.Info().Msg("replacing peer connection")
// destroy previous peer connection
session.GetWebSocketPeer().Destroy()
}
peer := &WebSocketPeerCtx{
logger: logger,
session: session,
connection: connection,
}
session.SetWebSocketPeer(peer)