2020-11-02 04:09:48 +13:00
|
|
|
package handler
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) SessionCreated(session types.Session) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
// send sdp and id over to client
|
2020-11-01 10:43:47 +13:00
|
|
|
if err := h.signalProvide(session); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.Admin() {
|
|
|
|
// send screen configurations if admin
|
2020-11-01 10:43:47 +13:00
|
|
|
if err := h.screenConfigurations(session); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// send broadcast status if admin
|
|
|
|
if err := h.boradcastStatus(session); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
|
|
|
|
// TODO: Refactor.
|
|
|
|
members := []*message.MembersListEntry{}
|
|
|
|
for _, session := range h.sessions.Members() {
|
|
|
|
members = append(members, &message.MembersListEntry{
|
|
|
|
ID: session.ID(),
|
|
|
|
Name: session.Name(),
|
|
|
|
Admin: session.Admin(),
|
|
|
|
Muted: session.Muted(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-23 03:54:50 +13:00
|
|
|
// send list of members to session
|
|
|
|
if err := session.Send(message.MembersList{
|
|
|
|
Event: event.MEMBER_LIST,
|
2020-11-02 04:09:48 +13:00
|
|
|
Memebers: members,
|
2020-10-23 03:54:50 +13:00
|
|
|
}); err != nil {
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Warn().Str("id", session.ID()).Err(err).Msgf("sending event %s has failed", event.MEMBER_LIST)
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// send screen current resolution
|
2020-11-01 10:43:47 +13:00
|
|
|
if err := h.screenResolution(session); err != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell session there is a host
|
2020-11-01 11:53:19 +13:00
|
|
|
host := h.sessions.GetHost()
|
|
|
|
if host != nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
if err := session.Send(message.Control{
|
|
|
|
Event: event.CONTROL_LOCKED,
|
|
|
|
ID: host.ID(),
|
|
|
|
}); err != nil {
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Warn().Str("id", session.ID()).Err(err).Msgf("sending event %s has failed", event.CONTROL_LOCKED)
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// let everyone know there is a new session
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.Member{
|
|
|
|
Event: event.MEMBER_CONNECTED,
|
2020-11-02 04:37:00 +13:00
|
|
|
ID: session.ID(),
|
|
|
|
Name: session.Name(),
|
|
|
|
Admin: session.Admin(),
|
|
|
|
Muted: session.Muted(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) SessionDestroyed(id string) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
// clear host if exists
|
2020-11-01 11:53:19 +13:00
|
|
|
host := h.sessions.GetHost()
|
|
|
|
if host != nil && host.ID() == id {
|
2020-10-23 03:54:50 +13:00
|
|
|
h.sessions.ClearHost()
|
|
|
|
if err := h.sessions.Broadcast(message.Control{
|
|
|
|
Event: event.CONTROL_RELEASE,
|
2020-11-01 11:53:19 +13:00
|
|
|
ID: id,
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// let everyone know session disconnected
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.MemberDisconnected{
|
|
|
|
Event: event.MEMBER_DISCONNECTED,
|
2020-11-01 11:53:19 +13:00
|
|
|
ID: id,
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.MEMBER_DISCONNECTED)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|