update control WS.

This commit is contained in:
Miroslav Šedivý 2020-11-30 18:24:38 +01:00
parent 4bbf3bb039
commit 2e090eb54d
6 changed files with 42 additions and 95 deletions

View File

@ -44,9 +44,10 @@ func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(session) h.sessions.SetHost(session)
h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.ControlHost{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_HOST,
ID: session.ID(), HasHost: true,
HostID: session.ID(),
}, nil) }, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
@ -62,9 +63,10 @@ func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) {
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.ControlHost{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_HOST,
ID: session.ID(), HasHost: false,
HostID: session.ID(),
}, nil) }, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
@ -76,9 +78,10 @@ func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(session) h.sessions.SetHost(session)
h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.ControlHost{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_HOST,
ID: session.ID(), HasHost: true,
HostID: session.ID(),
}, nil) }, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
@ -99,9 +102,10 @@ func (h *RoomHandler) controlGive(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(target) h.sessions.SetHost(target)
h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.ControlHost{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_HOST,
ID: target.ID(), HasHost: true,
HostID: target.ID(),
}, nil) }, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
@ -117,9 +121,9 @@ func (h *RoomHandler) controlReset(w http.ResponseWriter, r *http.Request) {
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.ControlHost{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_HOST,
ID: host.ID(), HasHost: false,
}, nil) }, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)

View File

@ -19,10 +19,7 @@ const (
) )
const ( const (
CONTROL_LOCKED = "control/locked" // TODO: Remove. CONTROL_HOST = "control/host"
CONTROL_REQUESTING = "control/requesting" // TODO: Remove.
CONTROL_GIVE = "control/give" // TODO: Remove.
CONTROL_HOST = "control/host" // TODO: New.
CONTROL_RELEASE = "control/release" CONTROL_RELEASE = "control/release"
CONTROL_REQUEST = "control/request" CONTROL_REQUEST = "control/request"
CONTROL_MOVE = "control/move" // TODO: New. (fallback) CONTROL_MOVE = "control/move" // TODO: New. (fallback)

View File

@ -52,7 +52,6 @@ type MemberData struct {
} }
// Control // Control
// TODO: New.
type ControlHost struct { type ControlHost struct {
Event string `json:"event,omitempty"` Event string `json:"event,omitempty"`
HasHost bool `json:"has_host"` HasHost bool `json:"has_host"`
@ -144,16 +143,3 @@ type MemberDisconnected struct {
Event string `json:"event"` Event string `json:"event"`
ID string `json:"id"` ID string `json:"id"`
} }
// TODO: Remove.
type Control struct {
Event string `json:"event"`
ID string `json:"id"`
}
// TODO: Remove.
type ControlTarget struct {
Event string `json:"event"`
ID string `json:"id"`
Target string `json:"target"`
}

View File

@ -12,71 +12,35 @@ func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
return nil return nil
} }
h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE)
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.ControlHost{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_HOST,
ID: session.ID(), HasHost: false,
}, nil) }, nil)
return nil return nil
} }
func (h *MessageHandlerCtx) controlRequest(session types.Session) error { func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
// TODO: Allow implicit requests.
host := h.sessions.GetHost() host := h.sessions.GetHost()
if host != nil {
if host == nil {
// set host
h.sessions.SetHost(session)
// let everyone know
h.sessions.Broadcast(
message.Control{
Event: event.CONTROL_LOCKED,
ID: session.ID(),
}, nil)
} else {
// tell session there is a host // tell session there is a host
if err := session.Send( return session.Send(
message.Control{ message.ControlHost{
Event: event.CONTROL_REQUEST, Event: event.CONTROL_HOST,
ID: host.ID(), HasHost: true,
}); err != nil { HostID: host.ID(),
return err
}
// tell host session wants to be host
return host.Send(
message.Control{
Event: event.CONTROL_REQUESTING,
ID: session.ID(),
}) })
} }
return nil h.sessions.SetHost(session)
}
func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.Control) error {
if !session.IsHost() {
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
return nil
}
target, ok := h.sessions.Get(payload.ID)
if !ok {
h.logger.Debug().Str("id", payload.ID).Msg("can't find target session")
return nil
}
h.sessions.SetHost(target)
h.sessions.Broadcast( h.sessions.Broadcast(
message.ControlTarget{ message.ControlHost{
Event: event.CONTROL_GIVE, Event: event.CONTROL_HOST,
ID: session.ID(), HasHost: true,
Target: target.ID(), HostID: session.ID(),
}, nil) }, nil)
return nil return nil

View File

@ -58,11 +58,6 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
err = h.controlRelease(session) err = h.controlRelease(session)
case event.CONTROL_REQUEST: case event.CONTROL_REQUEST:
err = h.controlRequest(session) err = h.controlRequest(session)
case event.CONTROL_GIVE:
payload := &message.Control{}
err = utils.Unmarshal(payload, raw, func() error {
return h.controlGive(session, payload)
})
// Screen Events // Screen Events
case event.SCREEN_SET: case event.SCREEN_SET:

View File

@ -39,9 +39,10 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
host := h.sessions.GetHost() host := h.sessions.GetHost()
if host != nil { if host != nil {
if err := session.Send( if err := session.Send(
message.Control{ message.ControlHost{
Event: event.CONTROL_LOCKED, Event: event.CONTROL_HOST,
ID: host.ID(), HasHost: true,
HostID: host.ID(),
}); err != nil { }); err != nil {
return err return err
} }
@ -65,9 +66,9 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast( h.sessions.Broadcast(
message.Control{ message.ControlHost{
Event: event.CONTROL_RELEASE, Event: event.CONTROL_HOST,
ID: session.ID(), HasHost: false,
}, nil) }, nil)
} }