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
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/kataras/go-events"
|
"github.com/kataras/go-events"
|
||||||
@ -36,21 +37,24 @@ type SessionManagerCtx struct {
|
|||||||
emmiter events.EventEmmiter
|
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()
|
manager.membersMu.Lock()
|
||||||
defer manager.membersMu.Unlock()
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
|
id, err := utils.NewUID(32)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
session := &SessionCtx{
|
session := &SessionCtx{
|
||||||
id: id,
|
id: id,
|
||||||
manager: manager,
|
manager: manager,
|
||||||
logger: manager.logger.With().Str("id", id).Logger(),
|
logger: manager.logger.With().Str("id", id).Logger(),
|
||||||
profile: MemberProfile{
|
profile: profile,
|
||||||
is_admin: admin,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
manager.members[id] = session
|
manager.members[id] = session
|
||||||
return session
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
|
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
|
return session, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *SessionManagerCtx) Destroy(id string) error {
|
func (manager *SessionManagerCtx) Delete(id string) error {
|
||||||
manager.membersMu.Lock()
|
manager.membersMu.Lock()
|
||||||
defer manager.membersMu.Unlock()
|
defer manager.membersMu.Unlock()
|
||||||
|
|
||||||
session, ok := manager.members[id]
|
session, ok := manager.members[id]
|
||||||
if ok {
|
if !ok {
|
||||||
|
return fmt.Errorf("Member not found.")
|
||||||
|
}
|
||||||
|
|
||||||
delete(manager.members, id)
|
delete(manager.members, id)
|
||||||
return session.destroy()
|
|
||||||
|
if session.Connected() {
|
||||||
|
return session.Disconnect("member deleted")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -70,9 +70,6 @@ func (session *SessionCtx) SetWebSocketConnected(connected bool) {
|
|||||||
session.manager.emmiter.Emit("disconnected", session)
|
session.manager.emmiter.Emit("disconnected", session)
|
||||||
|
|
||||||
session.websocket_peer = nil
|
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 {
|
func (session *SessionCtx) Disconnect(reason string) error {
|
||||||
// TODO: End WebSocket connection.
|
if err := session.Send(
|
||||||
// TODO: End WebRTC connection.
|
|
||||||
|
|
||||||
return session.Send(
|
|
||||||
message.Disconnect{
|
message.Disconnect{
|
||||||
Event: event.SYSTEM_DISCONNECT,
|
Event: event.SYSTEM_DISCONNECT,
|
||||||
Message: reason,
|
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 {
|
func (session *SessionCtx) Send(v interface{}) error {
|
||||||
@ -121,19 +131,3 @@ func (session *SessionCtx) SignalAnswer(sdp string) error {
|
|||||||
|
|
||||||
return session.webrtc_peer.SignalAnswer(sdp)
|
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 {
|
type SessionManager interface {
|
||||||
New(id string, admin bool) Session
|
Create(profile MemberProfile) (Session, error)
|
||||||
Get(id string) (Session, bool)
|
Get(id string) (Session, bool)
|
||||||
Destroy(id string) error
|
Delete(id string) error
|
||||||
|
|
||||||
HasHost() bool
|
HasHost() bool
|
||||||
SetHost(host Session)
|
SetHost(host Session)
|
||||||
|
@ -49,13 +49,14 @@ func (h *MessageHandlerCtx) Connected(session types.Session, websocket_peer type
|
|||||||
return true, ""
|
return true, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove, unused.
|
||||||
func (h *MessageHandlerCtx) Disconnected(id string) error {
|
func (h *MessageHandlerCtx) Disconnected(id string) error {
|
||||||
// TODO: Refactor.
|
// TODO: Refactor.
|
||||||
if h.locked && len(h.sessions.Admins()) == 0 {
|
if h.locked && len(h.sessions.Admins()) == 0 {
|
||||||
h.locked = false
|
h.locked = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return h.sessions.Destroy(id)
|
return h.sessions.Delete(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
|
func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user