mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
Create + Delete session.
This commit is contained in:
parent
104d98d129
commit
665d9d2130
@ -1,6 +1,7 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/kataras/go-events"
|
||||
@ -36,21 +37,24 @@ type SessionManagerCtx struct {
|
||||
emmiter events.EventEmmiter
|
||||
}
|
||||
|
||||
func (manager *SessionManagerCtx) New(id string, admin bool) types.Session {
|
||||
func (manager *SessionManagerCtx) Create(profile MemberProfile) (types.Session, error) {
|
||||
manager.membersMu.Lock()
|
||||
defer manager.membersMu.Unlock()
|
||||
|
||||
id, err := utils.NewUID(32)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session := &SessionCtx{
|
||||
id: id,
|
||||
manager: manager,
|
||||
logger: manager.logger.With().Str("id", id).Logger(),
|
||||
profile: MemberProfile{
|
||||
is_admin: admin,
|
||||
},
|
||||
profile: profile,
|
||||
}
|
||||
|
||||
manager.members[id] = session
|
||||
return session
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
||||
@ -61,14 +65,19 @@ func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
||||
return session, ok
|
||||
}
|
||||
|
||||
func (manager *SessionManagerCtx) Destroy(id string) error {
|
||||
func (manager *SessionManagerCtx) Delete(id string) error {
|
||||
manager.membersMu.Lock()
|
||||
defer manager.membersMu.Unlock()
|
||||
|
||||
session, ok := manager.members[id]
|
||||
if ok {
|
||||
if !ok {
|
||||
return fmt.Errorf("Member not found.")
|
||||
}
|
||||
|
||||
delete(manager.members, id)
|
||||
return session.destroy()
|
||||
|
||||
if session.Connected() {
|
||||
return session.Disconnect("member deleted")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -70,9 +70,6 @@ func (session *SessionCtx) SetWebSocketConnected(connected bool) {
|
||||
session.manager.emmiter.Emit("disconnected", session)
|
||||
|
||||
session.websocket_peer = nil
|
||||
|
||||
// TODO: Refactor.
|
||||
_ = session.manager.Destroy(session.id)
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,14 +93,27 @@ func (session *SessionCtx) SetWebRTCConnected(connected bool) {
|
||||
}
|
||||
|
||||
func (session *SessionCtx) Disconnect(reason string) error {
|
||||
// TODO: End WebSocket connection.
|
||||
// TODO: End WebRTC connection.
|
||||
|
||||
return session.Send(
|
||||
if err := session.Send(
|
||||
message.Disconnect{
|
||||
Event: event.SYSTEM_DISCONNECT,
|
||||
Message: reason,
|
||||
})
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if session.websocket_peer != nil {
|
||||
if err := session.websocket_peer.Destroy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if session.webrtc_peer != nil {
|
||||
if err := session.webrtc_peer.Destroy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (session *SessionCtx) Send(v interface{}) error {
|
||||
@ -121,19 +131,3 @@ func (session *SessionCtx) SignalAnswer(sdp string) error {
|
||||
|
||||
return session.webrtc_peer.SignalAnswer(sdp)
|
||||
}
|
||||
|
||||
func (session *SessionCtx) destroy() error {
|
||||
if session.websocket_peer != nil {
|
||||
if err := session.websocket_peer.Destroy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if session.webrtc_peer != nil {
|
||||
if err := session.webrtc_peer.Destroy(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -19,9 +19,9 @@ type Session interface {
|
||||
}
|
||||
|
||||
type SessionManager interface {
|
||||
New(id string, admin bool) Session
|
||||
Create(profile MemberProfile) (Session, error)
|
||||
Get(id string) (Session, bool)
|
||||
Destroy(id string) error
|
||||
Delete(id string) error
|
||||
|
||||
HasHost() bool
|
||||
SetHost(host Session)
|
||||
|
@ -49,13 +49,14 @@ func (h *MessageHandlerCtx) Connected(session types.Session, websocket_peer type
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// TODO: Remove, unused.
|
||||
func (h *MessageHandlerCtx) Disconnected(id string) error {
|
||||
// TODO: Refactor.
|
||||
if h.locked && len(h.sessions.Admins()) == 0 {
|
||||
h.locked = false
|
||||
}
|
||||
|
||||
return h.sessions.Destroy(id)
|
||||
return h.sessions.Delete(id)
|
||||
}
|
||||
|
||||
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
|
||||
|
Loading…
Reference in New Issue
Block a user