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-12-05 10:44:13 +13:00
|
|
|
// send initialization information
|
|
|
|
if err := session.Send(message.SystemInit{
|
|
|
|
Event: event.SYSTEM_INIT,
|
|
|
|
ImplicitHosting: true,
|
|
|
|
Locks: h.locked,
|
|
|
|
}); err != nil {
|
|
|
|
h.logger.Warn().Str("id", id).Err(err).Msgf("sending event %s has failed", event.SYSTEM_INIT)
|
|
|
|
return err
|
2021-11-17 10:50:11 +13:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-14 23:37:59 +12:00
|
|
|
// 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
|
2020-02-27 01:46:10 +13:00
|
|
|
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 {
|
2020-02-27 01:46:10 +13:00
|
|
|
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()
|
2020-02-27 01:46:10 +13:00
|
|
|
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 {
|
2020-02-27 01:46:10 +13:00
|
|
|
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
|
2020-02-27 01:46:10 +13:00
|
|
|
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 {
|
2020-02-27 01:46:10 +13:00
|
|
|
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
|
|
|
|
}
|