neko/server/internal/websocket/session.go

105 lines
2.6 KiB
Go
Raw Normal View History

2020-01-21 03:38:07 +13:00
package websocket
import (
2020-01-25 04:47:37 +13:00
"n.eko.moe/neko/internal/types"
"n.eko.moe/neko/internal/types/event"
"n.eko.moe/neko/internal/types/message"
2020-01-21 03:38:07 +13:00
)
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) SessionCreated(id string, session types.Session) error {
2020-02-13 12:13:33 +13:00
// send sdp and id over to client
if err := h.signalProvide(id, session); err != nil {
2020-01-21 03:38:07 +13:00
return err
}
2020-02-11 18:15:59 +13:00
if session.Admin() {
2020-02-13 12:13:33 +13:00
// send screen configurations if admin
2020-02-11 18:15:59 +13:00
if err := h.screenConfigurations(id, session); err != nil {
return err
}
2020-09-27 11:10:34 +13:00
// send broadcast status if admin
if err := h.boradcastStatus(session); err != nil {
return err
}
// if locked, notify admin about that
if h.locked {
2021-01-19 01:26:01 +13:00
if err := session.Send(message.Admin{
Event: event.ADMIN_LOCK,
ID: id,
}); err != nil {
h.logger.Warn().Str("id", id).Err(err).Msgf("sending event %s has failed", event.ADMIN_LOCK)
return err
}
}
2020-02-11 18:15:59 +13:00
}
2020-01-21 03:38:07 +13:00
return nil
}
2020-01-25 04:47:37 +13:00
func (h *MessageHandler) SessionConnected(id string, session types.Session) error {
2020-01-21 03:38:07 +13:00
// send list of members to session
2020-01-21 05:22:24 +13:00
if err := session.Send(message.MembersList{
Event: event.MEMBER_LIST,
2020-01-25 04:47:37 +13:00
Memebers: h.sessions.Members(),
2020-01-21 03:38:07 +13:00
}); err != nil {
h.logger.Warn().Str("id", id).Err(err).Msgf("sending event %s has failed", event.MEMBER_LIST)
return err
}
// send screen current resolution
if err := h.screenResolution(id, session); err != nil {
return err
}
2020-01-21 03:38:07 +13:00
// tell session there is a host
host, ok := h.sessions.GetHost()
if ok {
if err := session.Send(message.Control{
2020-01-21 05:22:24 +13:00
Event: event.CONTROL_LOCKED,
2020-01-25 04:47:37 +13:00
ID: host.ID(),
2020-01-21 03:38:07 +13:00
}); err != nil {
h.logger.Warn().Str("id", id).Err(err).Msgf("sending event %s has failed", event.CONTROL_LOCKED)
return err
}
}
// let everyone know there is a new session
if err := h.sessions.Broadcast(
2020-01-21 03:38:07 +13:00
message.Member{
2020-01-25 04:47:37 +13:00
Event: event.MEMBER_CONNECTED,
Member: session.Member(),
2020-01-21 03:38:07 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
2020-01-21 03:38:07 +13:00
return err
}
return nil
}
func (h *MessageHandler) SessionDestroyed(id string) error {
// clear host if exists
if h.sessions.IsHost(id) {
h.sessions.ClearHost()
if err := h.sessions.Broadcast(message.Control{
2020-01-21 05:22:24 +13:00
Event: event.CONTROL_RELEASE,
ID: id,
2020-01-21 03:38:07 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
2020-01-21 03:38:07 +13:00
}
}
// let everyone know session disconnected
if err := h.sessions.Broadcast(
2020-01-21 03:38:07 +13:00
message.MemberDisconnected{
2020-01-21 05:22:24 +13:00
Event: event.MEMBER_DISCONNECTED,
ID: id,
2020-01-21 03:38:07 +13:00
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.MEMBER_DISCONNECTED)
2020-01-21 03:38:07 +13:00
return err
}
return nil
}