2020-01-19 12:30:09 +13:00
|
|
|
package session
|
2020-01-13 21:05:38 +13:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/pion/webrtc/v2"
|
|
|
|
)
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
type Session struct {
|
2020-01-21 03:38:07 +13:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"username"`
|
|
|
|
Admin bool `json:"admin"`
|
2020-01-21 05:22:24 +13:00
|
|
|
Muted bool `json:"-"`
|
2020-01-21 03:38:07 +13:00
|
|
|
connected bool
|
|
|
|
socket *websocket.Conn
|
|
|
|
peer *webrtc.PeerConnection
|
|
|
|
mu sync.Mutex
|
2020-01-13 21:05:38 +13:00
|
|
|
}
|
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
func (session *Session) RemoteAddr() *string {
|
|
|
|
if session.socket != nil {
|
|
|
|
address := session.socket.RemoteAddr().String()
|
|
|
|
return &address
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
// TODO: write to peer data channel
|
|
|
|
func (session *Session) Write(v interface{}) error {
|
|
|
|
session.mu.Lock()
|
|
|
|
defer session.mu.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:22:24 +13:00
|
|
|
func (session *Session) Kick(v interface{}) error {
|
|
|
|
if err := session.Send(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.destroy()
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
func (session *Session) Send(v interface{}) error {
|
2020-01-13 21:05:38 +13:00
|
|
|
session.mu.Lock()
|
|
|
|
defer session.mu.Unlock()
|
|
|
|
|
|
|
|
if session.socket != nil {
|
|
|
|
return session.socket.WriteJSON(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-19 12:30:09 +13:00
|
|
|
func (session *Session) destroy() error {
|
2020-01-13 21:05:38 +13:00
|
|
|
if session.peer != nil && session.peer.ConnectionState() == webrtc.PeerConnectionStateConnected {
|
|
|
|
if err := session.peer.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.socket != nil {
|
|
|
|
if err := session.socket.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 12:30:09 +13:00
|
|
|
|
2020-01-13 21:05:38 +13:00
|
|
|
return nil
|
|
|
|
}
|