new MemberProfile & session refactor.

This commit is contained in:
Miroslav Šedivý 2020-12-02 17:29:38 +01:00
parent 220e162f42
commit 0d2f3405bb
4 changed files with 122 additions and 62 deletions

View File

@ -12,14 +12,15 @@ type MemberCreatePayload struct {
}
type MemberDataPayload struct {
ID string `json:"id"`
Secret string `json:"secret,omitempty"`
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
//Enabled bool `json:"enabled"`
//CanControl bool `json:"can_control"`
//CanWatch bool `json:"can_watch"`
//ClipboardAccess bool `json:"clipboard_access"`
ID string `json:"id"`
Secret string `json:"secret,omitempty"`
Name string `json:"name"`
IsAdmin bool `json:"is_admin"`
CanLogin bool `json:"can_login"`
CanConnect bool `json:"can_connect"`
CanWatch bool `json:"can_watch"`
CanHost bool `json:"can_host"`
CanAccessClipboard bool `json:"can_access_clipboard"`
}
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {

View File

@ -238,6 +238,18 @@ func (manager *SessionManagerCtx) OnDisconnected(listener func(session types.Ses
})
}
func (manager *SessionManagerCtx) OnReceivingStarted(listener func(session types.Session)) {
manager.emmiter.On("receiving_started", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
func (manager *SessionManagerCtx) OnReceivingStopped(listener func(session types.Session)) {
manager.emmiter.On("receiving_stopped", func(payload ...interface{}) {
listener(payload[0].(*SessionCtx))
})
}
// ---
// config
// ---

View File

@ -23,6 +23,14 @@ func (session *SessionCtx) ID() string {
return session.id
}
// ---
// profile
// ---
func (session *SessionCtx) VerifySecret(secret string) bool {
return session.profile.Secret == secret
}
func (session *SessionCtx) Name() string {
return session.profile.Name
}
@ -31,55 +39,44 @@ func (session *SessionCtx) IsAdmin() bool {
return session.profile.IsAdmin
}
func (session *SessionCtx) CanLogin() bool {
return session.profile.CanLogin
}
func (session *SessionCtx) CanConnect() bool {
return session.profile.CanConnect
}
func (session *SessionCtx) CanWatch() bool {
return session.profile.CanWatch
}
func (session *SessionCtx) CanHost() bool {
return session.profile.CanHost
}
func (session *SessionCtx) CanAccessClipboard() bool {
return session.profile.CanAccessClipboard
}
func (session *SessionCtx) SetProfile(profile types.MemberProfile) {
session.profile = profile
}
// ---
// runtime
// ---
func (session *SessionCtx) IsHost() bool {
return session.manager.host != nil && session.manager.host.ID() == session.ID()
}
func (session *SessionCtx) VerifySecret(secret string) bool {
return session.profile.Secret == secret
}
func (session *SessionCtx) IsConnected() bool {
// TODO: Refactor.
return session.websocket_connected// && session.webrtc_connected
return session.websocket_connected
}
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) {
session.websocket_peer = websocket_peer
}
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
if connected {
session.websocket_connected = true
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_connected", session)
session.manager.emmiter.Emit("connected", session)
} else {
session.websocket_connected = false
// TODO: Refactor.
//session.manager.emmiter.Emit("websocket_disconnected", session)
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
}
}
func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
session.webrtc_peer = webrtc_peer
}
func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected {
session.webrtc_connected = true
session.manager.emmiter.Emit("webrtc_connected", session)
} else {
session.webrtc_connected = false
session.manager.emmiter.Emit("webrtc_disconnected", session)
session.webrtc_peer = nil
}
func (session *SessionCtx) IsReceiving() bool {
return session.webrtc_connected
}
func (session *SessionCtx) Disconnect(reason string) error {
@ -106,6 +103,25 @@ func (session *SessionCtx) Disconnect(reason string) error {
return nil
}
// ---
// webscoket
// ---
func (session *SessionCtx) SetWebSocketPeer(websocket_peer types.WebSocketPeer) {
session.websocket_peer = websocket_peer
}
func (session *SessionCtx) SetWebSocketConnected(connected bool) {
if connected {
session.websocket_connected = true
session.manager.emmiter.Emit("connected", session)
} else {
session.websocket_connected = false
session.manager.emmiter.Emit("disconnected", session)
session.websocket_peer = nil
}
}
func (session *SessionCtx) Send(v interface{}) error {
if session.websocket_peer == nil {
return nil
@ -114,6 +130,25 @@ func (session *SessionCtx) Send(v interface{}) error {
return session.websocket_peer.Send(v)
}
// ---
// webrtc
// ---
func (session *SessionCtx) SetWebRTCPeer(webrtc_peer types.WebRTCPeer) {
session.webrtc_peer = webrtc_peer
}
func (session *SessionCtx) SetWebRTCConnected(connected bool) {
if connected {
session.webrtc_connected = true
session.manager.emmiter.Emit("receiving_started", session)
} else {
session.webrtc_connected = false
session.manager.emmiter.Emit("receiving_stopped", session)
session.webrtc_peer = nil
}
}
func (session *SessionCtx) SignalAnswer(sdp string) error {
if session.webrtc_peer == nil {
return nil

View File

@ -3,29 +3,41 @@ package types
import "net/http"
type MemberProfile struct {
ID string
Secret string
Name string
IsAdmin bool
//Enabled bool
//CanControl bool
//CanWatch bool
//ClipboardAccess bool
Secret string
Name string
IsAdmin bool
CanLogin bool
CanConnect bool
CanWatch bool
CanHost bool
CanAccessClipboard bool
}
type Session interface {
ID() string
VerifySecret(secret string) bool
Name() string
IsAdmin() bool
CanLogin() bool
CanConnect() bool
CanWatch() bool
CanHost() bool
CanAccessClipboard() bool
SetProfile(profile MemberProfile)
IsHost() bool
IsConnected() bool
VerifySecret(secret string) bool
IsReceiving() bool
Disconnect(reason string) error
SetWebSocketPeer(websocket_peer WebSocketPeer)
SetWebSocketConnected(connected bool)
Send(v interface{}) error
SetWebRTCPeer(webrtc_peer WebRTCPeer)
SetWebRTCConnected(connected bool)
Disconnect(reason string) error
Send(v interface{}) error
SignalAnswer(sdp string) error
}
@ -47,8 +59,8 @@ type SessionManager interface {
OnHostCleared(listener func(session Session))
OnConnected(listener func(session Session))
OnDisconnected(listener func(session Session))
ImplicitHosting() bool
// auth
Authenticate(r *http.Request) (Session, error)
}