WS added session events.

This commit is contained in:
Miroslav Šedivý 2020-12-02 18:59:54 +01:00
parent 1f5c0a3547
commit f361d0c681
5 changed files with 149 additions and 18 deletions

View File

@ -13,9 +13,13 @@ const (
) )
const ( const (
MEMBER_CREATED = "member/created"
MEMBER_DELETED = "member/deleted"
MEMBER_CONNECTED = "member/connected" MEMBER_CONNECTED = "member/connected"
MEMBER_UPDATED = "member/updated" // TODO: New.
MEMBER_DISCONNECTED = "member/disconnected" MEMBER_DISCONNECTED = "member/disconnected"
MEMBER_RECEIVING_STARTED = "member/receiving/started"
MEMBER_RECEIVING_STOPPED = "member/receiving/stopped"
MEMBER_PROFILE_UPDATED = "member/profile/updated"
) )
const ( const (

View File

@ -54,11 +54,23 @@ type MemberID struct {
ID string `json:"id"` ID string `json:"id"`
} }
type MemberProfile struct {
Event string `json:"event,omitempty"`
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
CanLogin bool `json:"can_login"`
CanConnect bool `json:"can_connect"`
CanWatch bool `json:"can_watch"`
CanHost bool `json:"can_host"`
CanAccessClipboard bool `json:"can_access_clipboard"`
}
type MemberData struct { type MemberData struct {
Event string `json:"event,omitempty"` Event string `json:"event,omitempty"`
ID string `json:"id"` ID string `json:"id"`
Name string `json:"name"` Profile MemberProfile `json:"profile"`
IsAdmin bool `json:"is_admin"` IsConnected bool `json:"is_connected"`
IsReceiving bool `json:"is_receiving"`
} }
///////////////////////////// /////////////////////////////

View File

@ -6,6 +6,38 @@ import (
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
) )
func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
// TODO: Join structs?
h.sessions.Broadcast(
message.MemberData{
Event: event.MEMBER_CREATED,
ID: session.ID(),
Profile: message.MemberProfile{
Name: session.Name(),
IsAdmin: session.IsAdmin(),
CanLogin: session.CanLogin(),
CanConnect: session.CanConnect(),
CanWatch: session.CanWatch(),
CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(),
},
IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(),
}, nil)
return nil
}
func (h *MessageHandlerCtx) SessionDeleted(session types.Session) error {
h.sessions.Broadcast(
message.MemberID{
Event: event.MEMBER_DELETED,
ID: session.ID(),
}, nil);
return nil
}
func (h *MessageHandlerCtx) SessionConnected(session types.Session) error { func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
// start streaming, when first member connects // start streaming, when first member connects
if !h.capture.Streaming() { if !h.capture.Streaming() {
@ -22,14 +54,11 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
} }
} }
// let everyone know there is a new session
h.sessions.Broadcast( h.sessions.Broadcast(
message.MemberData{ message.MemberID{
Event: event.MEMBER_CONNECTED, Event: event.MEMBER_CONNECTED,
ID: session.ID(), ID: session.ID(),
Name: session.Name(), }, nil);
IsAdmin: session.IsAdmin(),
}, nil)
return nil return nil
} }
@ -52,7 +81,6 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
}, nil) }, nil)
} }
// let everyone know session disconnected
h.sessions.Broadcast( h.sessions.Broadcast(
message.MemberID{ message.MemberID{
Event: event.MEMBER_DISCONNECTED, Event: event.MEMBER_DISCONNECTED,
@ -61,3 +89,40 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
return nil return nil
} }
func (h *MessageHandlerCtx) SessionReceivingStarted(session types.Session) error {
h.sessions.Broadcast(
message.MemberID{
Event: event.MEMBER_RECEIVING_STARTED,
ID: session.ID(),
}, nil);
return nil
}
func (h *MessageHandlerCtx) SessionReceivingStopped(session types.Session) error {
h.sessions.Broadcast(
message.MemberID{
Event: event.MEMBER_RECEIVING_STOPPED,
ID: session.ID(),
}, nil);
return nil
}
func (h *MessageHandlerCtx) SessionProfileUpdated(session types.Session) error {
// TODO: Join structs?
h.sessions.Broadcast(
message.MemberProfile{
Event: event.MEMBER_PROFILE_UPDATED,
Name: session.Name(),
IsAdmin: session.IsAdmin(),
CanLogin: session.CanLogin(),
CanConnect: session.CanConnect(),
CanWatch: session.CanWatch(),
CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(),
}, nil)
return nil
}

View File

@ -25,10 +25,20 @@ func (h *MessageHandlerCtx) systemInit(session types.Session) error {
members := map[string]message.MemberData{} members := map[string]message.MemberData{}
for _, session := range h.sessions.Members() { for _, session := range h.sessions.Members() {
// TODO: Join structs?
members[session.ID()] = message.MemberData{ members[session.ID()] = message.MemberData{
ID: session.ID(), ID: session.ID(),
Profile: message.MemberProfile{
Name: session.Name(), Name: session.Name(),
IsAdmin: session.IsAdmin(), IsAdmin: session.IsAdmin(),
CanLogin: session.CanLogin(),
CanConnect: session.CanConnect(),
CanWatch: session.CanWatch(),
CanHost: session.CanHost(),
CanAccessClipboard: session.CanAccessClipboard(),
},
IsConnected: session.IsConnected(),
IsReceiving: session.IsReceiving(),
} }
} }

View File

@ -49,6 +49,22 @@ type WebSocketManagerCtx struct {
} }
func (ws *WebSocketManagerCtx) Start() { func (ws *WebSocketManagerCtx) Start() {
ws.sessions.OnCreated(func(session types.Session) {
if err := ws.handler.SessionCreated(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session created with an error")
} else {
ws.logger.Debug().Str("id", session.ID()).Msg("session created")
}
})
ws.sessions.OnDeleted(func(session types.Session) {
if err := ws.handler.SessionDeleted(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session deleted with an error")
} else {
ws.logger.Debug().Str("id", session.ID()).Msg("session deleted")
}
})
ws.sessions.OnConnected(func(session types.Session) { ws.sessions.OnConnected(func(session types.Session) {
if err := ws.handler.SessionConnected(session); err != nil { if err := ws.handler.SessionConnected(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session connected with an error") ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session connected with an error")
@ -65,6 +81,30 @@ func (ws *WebSocketManagerCtx) Start() {
} }
}) })
ws.sessions.OnReceivingStarted(func(session types.Session) {
if err := ws.handler.SessionReceivingStarted(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session receiving started with an error")
} else {
ws.logger.Debug().Str("id", session.ID()).Msg("session receiving started")
}
})
ws.sessions.OnReceivingStopped(func(session types.Session) {
if err := ws.handler.SessionReceivingStopped(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session receiving stopped with an error")
} else {
ws.logger.Debug().Str("id", session.ID()).Msg("session receiving stopped")
}
})
ws.sessions.OnProfileUpdated(func(session types.Session) {
if err := ws.handler.SessionProfileUpdated(session); err != nil {
ws.logger.Warn().Str("id", session.ID()).Err(err).Msg("session profile updated with an error")
} else {
ws.logger.Debug().Str("id", session.ID()).Msg("session profile updated")
}
})
go func() { go func() {
ws.logger.Info().Msg("clipboard loop started") ws.logger.Info().Msg("clipboard loop started")