-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathRTCDataChannel.js
176 lines (148 loc) · 4.77 KB
/
RTCDataChannel.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
import DOMException from 'node-domexception';
export default class _RTCDataChannel extends EventTarget {
#dataChannel;
#readyState;
#bufferedAmountLowThreshold;
#binaryType;
#maxPacketLifeTime;
#maxRetransmits;
#negotiated;
#ordered;
onbufferedamountlow;
onclose;
onclosing;
onerror;
onmessage;
onopen;
constructor(dataChannel, opts = {}) {
super();
this.#dataChannel = dataChannel;
this.#binaryType = 'arraybuffer';
this.#readyState = this.#dataChannel.isOpen() ? 'open' : 'connecting';
this.#bufferedAmountLowThreshold = 0;
this.#maxPacketLifeTime = opts.maxPacketLifeTime || null;
this.#maxRetransmits = opts.maxRetransmits || null;
this.#negotiated = opts.negotiated || false;
this.#ordered = opts.ordered || true;
// forward dataChannel events
this.#dataChannel.onOpen(() => {
this.#readyState = 'open';
this.dispatchEvent(new Event('open'));
});
this.#dataChannel.onClosed(() => {
this.#readyState = 'closed';
this.dispatchEvent(new Event('close'));
});
this.#dataChannel.onError((msg) => {
this.dispatchEvent(
new RTCErrorEvent('error', {
error: new RTCError(
{
errorDetail: 'data-channel-failure',
},
msg,
),
}),
);
});
this.#dataChannel.onBufferedAmountLow(() => {
this.dispatchEvent(new Event('bufferedamountlow'));
});
this.#dataChannel.onMessage((data) => {
if (ArrayBuffer.isView(data)) {
data = data.buffer;
}
this.dispatchEvent(new MessageEvent('message', { data }));
});
// forward events to properties
this.addEventListener('message', (e) => {
if (this.onmessage) this.onmessage(e);
});
this.addEventListener('bufferedamountlow', (e) => {
if (this.onbufferedamountlow) this.onbufferedamountlow(e);
});
this.addEventListener('error', (e) => {
if (this.onerror) this.onerror(e);
});
this.addEventListener('close', (e) => {
if (this.onclose) this.onclose(e);
});
this.addEventListener('closing', (e) => {
if (this.onclosing) this.onclosing(e);
});
this.addEventListener('open', (e) => {
if (this.onopen) this.onopen(e);
});
}
set binaryType(type) {
if (type !== 'blob' && type !== 'arraybuffer') {
throw new DOMException(
"Failed to set the 'binaryType' property on 'RTCDataChannel': Unknown binary type : " + type,
'TypeMismatchError',
);
}
this.#binaryType = type;
}
get binaryType() {
return this.#binaryType;
}
get bufferedAmount() {
return this.#dataChannel?.bufferedAmount() || 0;
}
get bufferedAmountLowThreshold() {
return this.#bufferedAmountLowThreshold;
}
set bufferedAmountLowThreshold(value) {
const number = Number(value) || 0;
this.#bufferedAmountLowThreshold = number;
this.#dataChannel?.setBufferedAmountLowThreshold(number);
}
get id() {
return this.#dataChannel?.getId() || null;
}
get label() {
return this.#dataChannel?.getLabel() || null;
}
get maxPacketLifeTime() {
return this.#maxPacketLifeTime;
}
get maxRetransmits() {
return this.#maxRetransmits;
}
get negotiated() {
return this.#negotiated;
}
get ordered() {
return this.#ordered;
}
get protocol() {
return this.#dataChannel?.getProtocol() || '';
}
get readyState() {
return this.#readyState;
}
send(data) {
if (this.#readyState !== 'open') {
throw new DOMException(
"Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'",
'InvalidStateError',
);
}
// Needs network error, type error implemented
if (typeof data === 'string') {
this.#dataChannel.sendMessage(data);
} else if (data instanceof Blob) {
data.arrayBuffer().then((ab) => {
this.#dataChannel.sendMessageBinary(new Uint8Array(ab));
});
} else {
this.#dataChannel.sendMessageBinary(new Uint8Array(data));
}
}
close() {
this.#readyState = 'closing';
this.dispatchEvent(new Event('closing'));
this.#dataChannel.close();
this.#dataChannel = null;
}
}