OnHostChanged event.

This commit is contained in:
Miroslav Šedivý 2021-03-13 21:37:00 +01:00
parent 3aea0c7bf5
commit a6b66e4d55
7 changed files with 28 additions and 86 deletions

View File

@ -6,8 +6,6 @@ import (
"github.com/go-chi/chi" "github.com/go-chi/chi"
"demodesk/neko/internal/http/auth" "demodesk/neko/internal/http/auth"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
"demodesk/neko/internal/utils" "demodesk/neko/internal/utils"
) )
@ -50,13 +48,6 @@ func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(session) h.sessions.SetHost(session)
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: session.ID(),
}, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
@ -75,12 +66,6 @@ func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) {
h.desktop.ResetKeys() h.desktop.ResetKeys()
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
}, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
@ -93,13 +78,6 @@ func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(session) h.sessions.SetHost(session)
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: session.ID(),
}, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
@ -119,13 +97,6 @@ func (h *RoomHandler) controlGive(w http.ResponseWriter, r *http.Request) {
h.sessions.SetHost(target) h.sessions.SetHost(target)
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: target.ID(),
}, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
@ -139,11 +110,5 @@ func (h *RoomHandler) controlReset(w http.ResponseWriter, r *http.Request) {
h.desktop.ResetKeys() h.desktop.ResetKeys()
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
}, nil)
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }

View File

@ -35,10 +35,6 @@ type SessionManagerCtx struct {
emmiter events.EventEmmiter emmiter events.EventEmmiter
} }
// ---
// sessions
// ---
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, error) { func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, error) {
manager.sessionsMu.Lock() manager.sessionsMu.Lock()
@ -125,10 +121,10 @@ func (manager *SessionManagerCtx) List() []types.Session {
func (manager *SessionManagerCtx) SetHost(host types.Session) { func (manager *SessionManagerCtx) SetHost(host types.Session) {
manager.hostMu.Lock() manager.hostMu.Lock()
defer manager.hostMu.Unlock()
manager.host = host manager.host = host
manager.emmiter.Emit("host", host) manager.hostMu.Unlock()
manager.emmiter.Emit("host_changed", host)
} }
func (manager *SessionManagerCtx) GetHost() types.Session { func (manager *SessionManagerCtx) GetHost() types.Session {
@ -139,12 +135,7 @@ func (manager *SessionManagerCtx) GetHost() types.Session {
} }
func (manager *SessionManagerCtx) ClearHost() { func (manager *SessionManagerCtx) ClearHost() {
manager.hostMu.Lock() manager.SetHost(nil)
defer manager.hostMu.Unlock()
host := manager.host
manager.host = nil
manager.emmiter.Emit("host_cleared", host)
} }
// --- // ---
@ -197,18 +188,6 @@ func (manager *SessionManagerCtx) AdminBroadcast(v interface{}, exclude interfac
// events // events
// --- // ---
func (manager *SessionManagerCtx) OnHost(listener func(session types.Session)) {
manager.emmiter.On("host", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnHostCleared(listener func(session types.Session)) {
manager.emmiter.On("host_cleared", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnCreated(listener func(session types.Session)) { func (manager *SessionManagerCtx) OnCreated(listener func(session types.Session)) {
manager.emmiter.On("created", func(payload ...interface{}) { manager.emmiter.On("created", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx)) listener(payload[0].(*SessionCtx))
@ -245,6 +224,16 @@ func (manager *SessionManagerCtx) OnStateChanged(listener func(session types.Ses
}) })
} }
func (manager *SessionManagerCtx) OnHostChanged(listener func(session types.Session)) {
manager.emmiter.On("host_changed", func(payload ...interface{}) {
if payload[0] == nil {
listener(nil)
} else {
listener(payload[0].(*SessionCtx))
}
})
}
// --- // ---
// config // config
// --- // ---

View File

@ -63,12 +63,6 @@ func (session *SessionCtx) GetProfile() types.MemberProfile {
func (session *SessionCtx) profileChanged() { func (session *SessionCtx) profileChanged() {
if !session.CanHost() && session.IsHost() { if !session.CanHost() && session.IsHost() {
session.manager.ClearHost() session.manager.ClearHost()
session.manager.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
}, nil)
} }
if !session.CanWatch() && session.state.IsWatching { if !session.CanWatch() && session.state.IsWatching {

View File

@ -72,14 +72,13 @@ type SessionManager interface {
Broadcast(v interface{}, exclude interface{}) Broadcast(v interface{}, exclude interface{})
AdminBroadcast(v interface{}, exclude interface{}) AdminBroadcast(v interface{}, exclude interface{})
OnHost(listener func(session Session))
OnHostCleared(listener func(session Session))
OnCreated(listener func(session Session)) OnCreated(listener func(session Session))
OnDeleted(listener func(session Session)) OnDeleted(listener func(session Session))
OnConnected(listener func(session Session)) OnConnected(listener func(session Session))
OnDisconnected(listener func(session Session)) OnDisconnected(listener func(session Session))
OnProfileChanged(listener func(session Session)) OnProfileChanged(listener func(session Session))
OnStateChanged(listener func(session Session)) OnStateChanged(listener func(session Session))
OnHostChanged(listener func(session Session))
ImplicitHosting() bool ImplicitHosting() bool

View File

@ -20,12 +20,6 @@ func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
h.desktop.ResetKeys() h.desktop.ResetKeys()
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
}, nil)
return nil return nil
} }
@ -53,12 +47,6 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
} }
h.sessions.SetHost(session) h.sessions.SetHost(session)
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: session.ID(),
}, nil)
return nil return nil
} }

View File

@ -47,12 +47,6 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
if session.IsHost() { if session.IsHost() {
h.desktop.ResetKeys() h.desktop.ResetKeys()
h.sessions.ClearHost() h.sessions.ClearHost()
h.sessions.Broadcast(
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
}, nil)
} }
return h.SessionStateChanged(session) return h.SessionStateChanged(session)

View File

@ -92,6 +92,19 @@ func (manager *WebSocketManagerCtx) Start() {
} }
}) })
manager.sessions.OnHostChanged(func(session types.Session) {
msg := message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: session != nil,
}
if msg.HasHost {
msg.HostID = session.ID()
}
manager.sessions.Broadcast(msg, nil)
})
manager.desktop.OnClipboardUpdated(func() { manager.desktop.OnClipboardUpdated(func() {
session := manager.sessions.GetHost() session := manager.sessions.GetHost()
if session == nil || !session.CanAccessClipboard() { if session == nil || !session.CanAccessClipboard() {