Archived
2
0
This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
neko-custom/server/internal/websocket/session.go

106 lines
2.6 KiB
Go
Raw Permalink Normal View History

2020-01-21 03:38:07 +13:00
package websocket
import (
2021-10-06 09:38:24 +13:00
"m1k1o/neko/internal/types"
"m1k1o/neko/internal/types/event"
"m1k1o/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
}
2021-11-17 10:50:11 +13:00
// notify all about what is locked
for resource, id := range h.locked {
if err := session.Send(message.AdminLock{
Event: event.ADMIN_LOCK,
ID: id,
Resource: resource,
}); 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
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
}
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
}