neko/server/internal/websocket/session.go

79 lines
2.0 KiB
Go
Raw Normal View History

2020-01-21 03:38:07 +13:00
package websocket
import (
"n.eko.moe/neko/internal/event"
"n.eko.moe/neko/internal/message"
"n.eko.moe/neko/internal/session"
)
func (h *MessageHandler) SessionCreated(id string, session *session.Session) error {
if err := session.Send(message.Identity{
2020-01-21 05:22:24 +13:00
Event: event.IDENTITY_PROVIDE,
ID: id,
2020-01-21 03:38:07 +13:00
}); err != nil {
return err
}
return nil
}
func (h *MessageHandler) SessionConnected(id string, session *session.Session) error {
// 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-21 03:38:07 +13:00
Memebers: h.sessions.GetConnected(),
}); err != nil {
h.logger.Warn().Str("id", id).Err(err).Msgf("sending event %s has failed", event.MEMBER_LIST)
return err
}
// 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,
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.Brodcast(
message.Member{
2020-01-21 05:22:24 +13:00
Event: event.MEMBER_CONNECTED,
2020-01-21 03:38:07 +13:00
Session: session,
}, nil); err != nil {
h.logger.Warn().Err(err).Msgf("brodcasting event %s has failed", event.CONTROL_RELEASE)
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.Brodcast(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("brodcasting event %s has failed", event.CONTROL_RELEASE)
}
}
// let everyone know session disconnected
if err := h.sessions.Brodcast(
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("brodcasting event %s has failed", event.MEMBER_DISCONNECTED)
return err
}
return nil
}