neko/internal/session/session.go

131 lines
2.3 KiB
Go
Raw Normal View History

package session
import (
"github.com/rs/zerolog"
2020-10-30 06:07:04 +13:00
2020-10-29 07:15:48 +13:00
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
type Session struct {
logger zerolog.Logger
id string
name string
admin bool
muted bool
connected bool
manager *SessionManager
socket types.WebSocket
peer types.Peer
}
func (session *Session) ID() string {
return session.id
}
func (session *Session) Name() string {
return session.name
}
func (session *Session) Admin() bool {
return session.admin
}
func (session *Session) Muted() bool {
return session.muted
}
func (session *Session) IsHost() bool {
return session.manager.host == session.id
}
func (session *Session) Connected() bool {
return session.connected
}
func (session *Session) Address() string {
if session.socket == nil {
return ""
}
2020-11-01 09:46:29 +13:00
return session.socket.Address()
}
func (session *Session) Member() *types.Member {
return &types.Member{
ID: session.id,
Name: session.name,
Admin: session.admin,
Muted: session.muted,
}
}
func (session *Session) SetMuted(muted bool) {
session.muted = muted
}
2020-11-01 09:46:29 +13:00
func (session *Session) SetName(name string) {
session.name = name
}
2020-11-01 09:46:29 +13:00
func (session *Session) SetSocket(socket types.WebSocket) {
session.socket = socket
}
2020-11-01 09:46:29 +13:00
func (session *Session) SetPeer(peer types.Peer) {
session.peer = peer
}
2020-11-01 09:46:29 +13:00
func (session *Session) SetConnected() {
session.connected = true
session.manager.emmiter.Emit("connected", session.id, session)
}
func (session *Session) Disconnect(reason string) error {
if session.socket == nil {
return nil
}
if err := session.socket.Send(&message.Disconnect{
Event: event.SYSTEM_DISCONNECT,
Message: reason,
}); err != nil {
return err
}
return session.manager.Destroy(session.id)
}
func (session *Session) Send(v interface{}) error {
if session.socket == nil {
return nil
}
2020-11-01 09:46:29 +13:00
return session.socket.Send(v)
}
func (session *Session) SignalAnswer(sdp string) error {
if session.peer == nil {
return nil
}
2020-11-01 09:46:29 +13:00
return session.peer.SignalAnswer(sdp)
}
func (session *Session) destroy() error {
if session.socket != nil {
if err := session.socket.Destroy(); err != nil {
return err
}
}
if session.peer != nil {
if err := session.peer.Destroy(); err != nil {
return err
}
}
return nil
}