neko/server/internal/websocket/handler/session.go

112 lines
2.9 KiB
Go
Raw Permalink Normal View History

2022-09-13 08:36:56 +12:00
package handler
2020-01-21 03:38:07 +13:00
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: h.webrtc.ImplicitControl(),
2022-09-14 06:04:43 +12:00
Locks: h.state.AllLocked(),
2022-11-20 08:26:45 +13:00
FileTransfer: h.state.FileTransferEnabled(),
2021-12-05 10:44:13 +13:00
}); 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
}
2022-11-20 08:26:45 +13:00
// send file list if file transfer is enabled
if h.state.FileTransferEnabled() && (session.Admin() || !h.state.IsLocked("file_transfer")) {
if err := h.FileTransferRefresh(session); err != nil {
return err
}
}
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{
2022-11-20 08:26:45 +13:00
Event: event.MEMBER_LIST,
Members: 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
}