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 {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Admin bool
|
2020-01-13 21:05:38 +13:00
|
|
|
socket *websocket.Conn
|
|
|
|
peer *webrtc.PeerConnection
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|