-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.js
183 lines (153 loc) · 4.61 KB
/
Connection.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const log = console.log
export default class Connection {
constructor(peerA, peerB, ws){
this.peerA = peerA
this.peerB = peerB
this.ws = ws
this.A = {}
this.B = {}
this.servers = null
this.dataChannelParams = {
ordered: false
}
}
////////////
// Peer A //
////////////
async peerA_init(){
// Create a RTCPeerConnection for peerA
this.A.rtc = new RTCPeerConnection(this.servers)
// Setup ICE Listeners
this.A.rtc.onicecandidate = this.peerA_onIceCandidate.bind(this)
console.log('Created a RTC: ', this.A.rtc)
// Create a DataChannel for peerA
this.A.rtc.ondatachannel = this.peerA_onDataChannel.bind(this)
this.A.dc = this.A.rtc.createDataChannel("dc", this.dataChannelParams)
this.A.dc.binaryType = 'arraybuffer'
// Setup DataChannel Listeners
this.A.dc.onopen = this.peerA_onOpen.bind(this)
this.A.dc.onclose = this.peerA_onClose.bind(this)
this.A.dc.onmessage = this.peerA_onMessage.bind(this)
console.log('Created a DataChannel', this.A.dc)
// Once we've set the localDescription, return
return new Promise((resolve, reject) => {
// Create an Offer for peerA
this.A.rtc.createOffer().then(offer => {
this.A.rtc.setLocalDescription(offer).then(()=>{
console.log("local description set: ", this.A.rtc.localDescription)
resolve(this.A.rtc.localDescription)
})
}, error => {
console.error("Error creating an offer", error)
})
})
}
peerA_onOpen(event){
log("peerA_onOpen: ", event)
this.peerA.onConnected(this.peerB, this.A.dc)
// this.A.dc.send("FUCK")
}
peerA_onClose(event){
log("peerA_onClose: ", event)
}
peerA_onMessage(event){
log("peerA_onMessage: ", event)
}
peerA_onDataChannel(event){
log("peerA_onDataChannel: ", event)
this.A.dc.send("AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH")
}
onPeerBAnswer(desc) {
console.log("setting peerA remote description")
return this.A.rtc.setRemoteDescription(desc).catch(error => {
console.error(error)
})
}
onPeerBICECandidate(candidate) {
console.log("onPeerBICECandidate")
return this.A.rtc.addIceCandidate(candidate).catch(error => {
console.error(error)
})
}
peerA_onIceCandidate(event) {
log("peerA_onIceCandidate: ", event)
if(event.candidate){
this.ws.send(JSON.stringify({
method: 'peerCandidate',
from: this.peerA.id,
to: this.peerB.id,
candidate: event.candidate
}))
}
}
////////////
// Peer B //
////////////
async peerB_init(){
// Create a RTCPeerConnection for peerB
this.B.rtc = new RTCPeerConnection(this.servers)
// Setup DataChannel Listeners
this.B.rtc.ondatachannel = this.peerB_onDataChannel.bind(this)
// Setup ICE Listeners
this.B.rtc.onicecandidate = this.peerB_onIceCandidate.bind(this)
console.log('Created a RTC: ', this.B.rtc)
// Find the correct connection
let description = null
this.peerA.connections.forEach(connection => {
if(connection.b.peer_id == this.peerB.id){
description = connection.a.localDescription
}
})
console.log("setting peerB remote description: ", description)
await this.B.rtc.setRemoteDescription(description)
let desc = await this.B.rtc.createAnswer()
console.log("setting peerB local description: ", desc)
this.B.rtc.setLocalDescription(desc)
return desc
}
peerB_onOpen(event){
log("peerB_onOpen: ", event)
}
peerB_onClose(event){
log("peerB_onClose: ", event)
}
peerB_onMessage(event){
log("peerB_onMessage: ", event)
}
peerB_onDataChannel(event){
log("GOT A FUCKING DATA CHANNEL!", event)
this.B.dc = event.channel
this.B.dc.onmessage = this.peerB_onMessage.bind(this)
this.peerB.onConnected(this.peerA, this.B.dc)
this.B.dc.send("FUCK you!!!!!!!!!!!!")
}
peerB_onIceCandidate(event) {
console.log("peerB_onIceCandidate: ", event)
if(event.candidate){
this.ws.send(JSON.stringify({
method: 'peerCandidate',
from: this.peerB.id,
to: this.peerA.id,
candidate: event.candidate
}))
}
}
onPeerAICECandidate(candidate) {
console.log("onPeerAICECandidate")
return this.B.rtc.addIceCandidate(candidate).catch(error => {
console.error(error)
})
}
serialize(){
return {
a: {
peer_id: this.peerA.id,
localDescription: this.A.rtc ? this.A.rtc.localDescription : null,
},
b: {
peer_id: this.peerB.id,
localDescription: this.B.rtc ? this.B.localDescription : null,
}
}
}
}