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

View File

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

View File

@ -52,7 +52,6 @@ type MemberData struct {
}
// Control
// TODO: New.
type ControlHost struct {
Event string `json:"event,omitempty"`
HasHost bool `json:"has_host"`
@ -144,16 +143,3 @@ type MemberDisconnected struct {
Event string `json:"event"`
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
}
h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE)
h.sessions.ClearHost()
h.sessions.Broadcast(
message.Control{
Event: event.CONTROL_RELEASE,
ID: session.ID(),
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
}, nil)
return nil
}
func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
// TODO: Allow implicit requests.
host := h.sessions.GetHost()
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 {
if host != nil {
// tell session there is a host
if err := session.Send(
message.Control{
Event: event.CONTROL_REQUEST,
ID: host.ID(),
}); err != nil {
return err
}
// tell host session wants to be host
return host.Send(
message.Control{
Event: event.CONTROL_REQUESTING,
ID: session.ID(),
return session.Send(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: host.ID(),
})
}
return nil
}
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.SetHost(session)
h.sessions.Broadcast(
message.ControlTarget{
Event: event.CONTROL_GIVE,
ID: session.ID(),
Target: target.ID(),
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: session.ID(),
}, nil)
return nil

View File

@ -58,11 +58,6 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
err = h.controlRelease(session)
case event.CONTROL_REQUEST:
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
case event.SCREEN_SET:

View File

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