-
Notifications
You must be signed in to change notification settings - Fork 16
/
conn.go
117 lines (103 loc) · 3.33 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package peer
import (
"github.com/muka/peerjs-go/emitter"
"github.com/muka/peerjs-go/models"
"github.com/pion/webrtc/v3"
"github.com/sirupsen/logrus"
)
//Connection shared interface
type Connection interface {
GetType() string
GetID() string
GetPeerID() string
GetProvider() *Peer
GetMetadata() interface{}
GetPeerConnection() *webrtc.PeerConnection
SetPeerConnection(pc *webrtc.PeerConnection)
Close() error
HandleMessage(*models.Message) error
Emit(string, interface{})
GetOptions() ConnectionOptions
}
func newBaseConnection(connType string, peer *Peer, opts ConnectionOptions) BaseConnection {
return BaseConnection{
Emitter: emitter.NewEmitter(),
Type: connType,
Provider: peer,
log: createLogger(connType, opts.Debug),
opts: opts,
negotiator: nil,
}
}
// BaseConnection shared base connection
type BaseConnection struct {
emitter.Emitter
// id connection ID
id string
// peerID peer ID of the connection
peerID string
//Provider is the peer instance
Provider *Peer
// DataChannel A reference to the RTCDataChannel object associated with the connection.
DataChannel *webrtc.DataChannel
// The optional label passed in or assigned by PeerJS when the connection was initiated.
Label string
// Metadata Any type of metadata associated with the connection, passed in by whoever initiated the connection.
Metadata interface{}
// Open This is true if the connection is open and ready for read/write.
Open bool
// PeerConnection A reference to the RTCPeerConnection object associated with the connection.
PeerConnection *webrtc.PeerConnection
// Reliable Whether the underlying data channels are reliable; defined when the connection was initiated.
Reliable bool
// Serialization The serialization format of the data sent over the connection. Can be binary (default), binary-utf8, json, or none.
Serialization string
// Type defines the type for connections
Type string
// BufferSize The number of messages queued to be sent once the browser buffer is no longer full.
BufferSize int
opts ConnectionOptions
log *logrus.Entry
negotiator *Negotiator
}
// GetOptions return the connection configuration
func (c *BaseConnection) GetOptions() ConnectionOptions {
return c.opts
}
// GetMetadata return the connection metadata
func (c *BaseConnection) GetMetadata() interface{} {
return c.Metadata
}
// GetPeerConnection return the underlying WebRTC PeerConnection
func (c *BaseConnection) GetPeerConnection() *webrtc.PeerConnection {
return c.PeerConnection
}
// SetPeerConnection set the underlying WebRTC PeerConnection
func (c *BaseConnection) SetPeerConnection(pc *webrtc.PeerConnection) {
c.PeerConnection = pc
c.log.Debugf("%v", c.PeerConnection)
}
// GetID return the connection ID
func (c *BaseConnection) GetID() string {
return c.id
}
// GetPeerID return the connection peer ID
func (c *BaseConnection) GetPeerID() string {
return c.peerID
}
// Close closes the data connection
func (c *BaseConnection) Close() error {
panic("Not implemented!")
}
// HandleMessage handles incoming messages
func (c *BaseConnection) HandleMessage(msg *models.Message) error {
panic("Not implemented!")
}
// GetType return the connection type
func (c *BaseConnection) GetType() string {
return c.Type
}
// GetProvider return the peer provider
func (c *BaseConnection) GetProvider() *Peer {
return c.Provider
}