diff --git a/internal/api/room/broadcast.go b/internal/api/room/broadcast.go index d7665663..d0639770 100644 --- a/internal/api/room/broadcast.go +++ b/internal/api/room/broadcast.go @@ -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) diff --git a/internal/api/room/screen.go b/internal/api/room/screen.go index ba70fa1b..b58b8948 100644 --- a/internal/api/room/screen.go +++ b/internal/api/room/screen.go @@ -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, diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index 9cfaec79..0649dd7d 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -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"` + 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"` + ID string `json:"id"` } type MemberProfile struct { - Event string `json:"event,omitempty"` - ID string `json:"id"` - *types.MemberProfile + ID string `json:"id"` + types.MemberProfile } type SessionState struct { - Event string `json:"event,omitempty"` - ID string `json:"id"` - *types.SessionState + ID string `json:"id"` + 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,29 +84,25 @@ 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"` + 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"` + X int16 `json:"x"` + Y int16 `json:"y"` } // TODO: New. type ControlKey struct { - Event string `json:"event,omitempty"` - Key uint32 `json:"key"` + Key uint32 `json:"key"` } ///////////////////////////// @@ -126,10 +110,9 @@ type ControlKey struct { ///////////////////////////// type ScreenSize struct { - Event string `json:"event,omitempty"` - Width int `json:"width"` - Height int `json:"height"` - Rate int16 `json:"rate"` + Width int `json:"width"` + Height int `json:"height"` + Rate int16 `json:"rate"` } ///////////////////////////// @@ -137,8 +120,7 @@ type ScreenSize struct { ///////////////////////////// type ClipboardData struct { - Event string `json:"event,omitempty"` - Text string `json:"text"` + Text string `json:"text"` } ///////////////////////////// @@ -146,15 +128,13 @@ 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"` + 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"` diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index cbad880b..efff4f7a 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -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") - } }) } diff --git a/internal/websocket/filechooserdialog.go b/internal/websocket/filechooserdialog.go index 9791ea4f..e753e786 100644 --- a/internal/websocket/filechooserdialog.go +++ b/internal/websocket/filechooserdialog.go @@ -22,10 +22,11 @@ func (manager *WebSocketManagerCtx) fileChooserDialogEvents() { activeSession = host - go manager.sessions.Broadcast(message.SessionID{ - Event: event.FILE_CHOOSER_DIALOG_OPENED, - ID: host.ID(), - }, nil) + go manager.sessions.Broadcast( + event.FILE_CHOOSER_DIALOG_OPENED, + message.SessionID{ + ID: host.ID(), + }, nil) }) // when dialog closes, everyone should be notified. @@ -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, - ID: activeSession.ID(), - }); err != nil { - logger.Warn().Err(err). - Str("event", event.FILE_CHOOSER_DIALOG_OPENED). - Msg("could not send event") - } + session.Send( + event.FILE_CHOOSER_DIALOG_OPENED, + message.SessionID{ + ID: activeSession.ID(), + }) }) // when user, that holds dialog, disconnects, it should be closed. diff --git a/internal/websocket/handler/control.go b/internal/websocket/handler/control.go index 14cc03b2..71c0f21a 100644 --- a/internal/websocket/handler/control.go +++ b/internal/websocket/handler/control.go @@ -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 } } diff --git a/internal/websocket/handler/screen.go b/internal/websocket/handler/screen.go index 07a6fc2c..4ae16230 100644 --- a/internal/websocket/handler/screen.go +++ b/internal/websocket/handler/screen.go @@ -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, diff --git a/internal/websocket/handler/send.go b/internal/websocket/handler/send.go index 73eac541..5a8be5af 100644 --- a/internal/websocket/handler/send.go +++ b/internal/websocket/handler/send.go @@ -15,22 +15,26 @@ func (h *MessageHandlerCtx) sendUnicast(session types.Session, payload *message. return nil } - return receiver.Send(message.SendUnicast{ - Event: event.SEND_UNICAST, - Sender: session.ID(), - Receiver: receiver.ID(), - Subject: payload.Subject, - Body: payload.Body, - }) -} - -func (h *MessageHandlerCtx) sendBroadcast(session types.Session, payload *message.SendBroadcast) error { - h.sessions.Broadcast(message.SendBroadcast{ - Event: event.SEND_BROADCAST, - Sender: session.ID(), - Subject: payload.Subject, - Body: payload.Body, - }, []string{session.ID()}) + 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( + event.SEND_BROADCAST, + message.SendBroadcast{ + Sender: session.ID(), + Subject: payload.Subject, + Body: payload.Body, + }, []string{session.ID()}) return nil } diff --git a/internal/websocket/handler/session.go b/internal/websocket/handler/session.go index f50d69f5..1d05dcc7 100644 --- a/internal/websocket/handler/session.go +++ b/internal/websocket/handler/session.go @@ -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,9 +20,9 @@ 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(), + ID: session.ID(), }, nil) return 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 diff --git a/internal/websocket/handler/signal.go b/internal/websocket/handler/signal.go index e32becd0..0e48b1f3 100644 --- a/internal/websocket/handler/signal.go +++ b/internal/websocket/handler/signal.go @@ -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, + 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 } diff --git a/internal/websocket/handler/system.go b/internal/websocket/handler/system.go index 59c2a63a..8f2d1a01 100644 --- a/internal/websocket/handler/system.go +++ b/internal/websocket/handler/system.go @@ -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 } diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 5686e2de..a81d1170 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -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, - 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") + session.Send( + event.CLIPBOARD_UPDATED, + message.ClipboardData{ + Text: data.Text, + // TODO: Send HTML? + }) }) 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{ + // TODO: Better handling... + raw, err := json.Marshal(message.SystemDisconnect{ + Message: err.Error(), + }) + + if err != nil { + manager.logger.Error().Err(err).Msg("failed to create disconnect event") + } + + err = connection.WriteJSON( + types.WebSocketMessage{ Event: event.SYSTEM_DISCONNECT, - Message: err.Error(), - }); err != nil { - logger.Error().Err(err).Msg("failed to send disconnect event") + 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)