-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebRTCConnection.js
133 lines (128 loc) · 5 KB
/
WebRTCConnection.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
/**
* Created by forge on 02.10.2017.
*/
class WebRTCConnection {
/**
* Simple, no bullshit WebRTC datachannel. offer, answer, complete, and send.
* @param {Function} doOnOpen
* @param {Function} doOnClose
* @param {Function} doOnStandby when the connection fails but may reopen again
* @param {Function} doOnMessage
* @param {int} timeout milliseconds
*/
constructor(doOnOpen, doOnClose, doOnStandby, doOnMessage, timeout = 1000*60*60*24){
this.peerConnection = new RTCPeerConnection(this.iceServers);
this._completeOnOpen = () => {}; //callback hook for complete promise
this.peerConnection.oniceconnectionstatechange = (event) => {
if (this.peerConnection.iceConnectionState == "closed") {
doOnClose(this);
}
if (this.peerConnection.iceConnectionState == "failed") {
doOnStandby(this);
}
if (this.peerConnection.iceConnectionState == "connected") {
this._completeOnOpen();
doOnOpen(this);
}
};
this.doOnMessage = (event) => doOnMessage(event.data);
this.timeout = timeout;
this.resolved = false;
this.verbose = true;
}
/**
* create a WebRTC offer sdp
* @return {Promise} {String} sdp
*/
offer(){
this.peerConnection.utilityDataChannel = this.peerConnection.createDataChannel(
"WebRTCConnectionUtility-1.0"
);
this.peerConnection.utilityDataChannel.onmessage = (event) => this.doOnMessage(event);
if(this.peerConnection.localDescription?.sdp) throw "offer or answer already made! (o)";
this.peerConnection.createOffer().then(description =>
this.peerConnection.setLocalDescription(description)
);
// timeout dropin \\
this._completeOnOpen = () => {this.resolved = true;};
setTimeout(() => {if(!this.resolved){
this.peerConnection.close();
if (this.verbose) throw "incomplete connection expired out after "+this.timeout+"ms. (o)";
}}, this.timeout);
// timeout dropin end \\
return new Promise((accept,reject)=>{
this.peerConnection.onicecandidate = event => {
if (event.candidate) return;
accept(this.peerConnection.localDescription.sdp);
}
});
}
/**
* create an answer from an offer sdp
* @param {String} sdp
* @return {Promise} {String} sdp
*/
answer(sdp){
this.peerConnection.ondatachannel = (event) => {
if (event.channel.label == "WebRTCConnectionUtility-1.0") {
this.peerConnection.utilityDataChannel = event.channel;
this.peerConnection.utilityDataChannel.onmessage = (event) => this.doOnMessage(event);
} else {
this.peerConnection.close();
throw "incompatible WebRTCConnections! (a)"
}
};
if(this.peerConnection.localDescription?.sdp) throw "offer or answer already made! (a)";
this.peerConnection.setRemoteDescription(
new RTCSessionDescription({
type:"offer",
sdp:sdp
})
).then(
() => this.peerConnection.createAnswer()
).then(
description => this.peerConnection.setLocalDescription(description)
);
// timeout dropin \\
this._completeOnOpen = () => {this.resolved = true;};
setTimeout(() => {if(!this.resolved){
this.peerConnection.close();
if (this.verbose) throw "incomplete connection expired out after "+this.timeout+"ms. (a)";
}}, this.timeout);
// timeout dropin end \\
return new Promise((accept,reject)=>{
this.peerConnection.onicecandidate = event => {
if (event.candidate) return;
accept(this.peerConnection.localDescription.sdp);
};
});
}
/**
* complete a connection
* @param {String} sdp
* @return {Promise} void on successfully established connection
*/
complete(sdp){
this.peerConnection.setRemoteDescription(
new RTCSessionDescription({
type: "answer",
sdp: sdp
})
);
return new Promise((accept, reject) => {
this._completeOnOpen = () => {this.resolved = true; setTimeout(()=>accept(),100);};
setTimeout(() => {if(!this.resolved){
this.peerConnection.close();
reject("incomplete connection expired out after "+this.timeout+"ms. (c)");
}}, this.timeout)
})
}
close(){
if(this.peerConnection.iceConnectionState != "closed"){
this.peerConnection.close();
}
}
send(data){
this.peerConnection.utilityDataChannel.send(data);
}
}