forked from murat-dogan/node-datachannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTCSctpTransport.ts
55 lines (45 loc) · 1.96 KB
/
RTCSctpTransport.ts
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import RTCDtlsTransport from './RTCDtlsTransport';
import RTCPeerConnection from './RTCPeerConnection';
export default class RTCSctpTransport extends EventTarget implements globalThis.RTCSctpTransport {
#pc: RTCPeerConnection = null;
#extraFunctions = null;
#transport: RTCDtlsTransport = null;
onstatechange: ((this: RTCSctpTransport, ev: Event) => any) | null = null;
constructor(initial: { pc: RTCPeerConnection, extraFunctions }) {
super();
this.#pc = initial.pc;
this.#extraFunctions = initial.extraFunctions;
this.#transport = new RTCDtlsTransport({ pc: initial.pc, extraFunctions: initial.extraFunctions });
// forward peerConnection events
this.#pc.addEventListener('connectionstatechange', () => {
this.dispatchEvent(new Event('statechange'));
});
// forward events to properties
this.addEventListener('statechange', (e) => {
if (this.onstatechange) this.onstatechange(e);
});
}
get maxChannels(): number | null {
if (this.state !== 'connected') return null;
return this.#pc ? this.#extraFunctions.maxDataChannelId() : 0;
}
get maxMessageSize(): number {
if (this.state !== 'connected') return null;
return this.#pc ? this.#extraFunctions.maxMessageSize() : 0;
}
get state(): globalThis.RTCSctpTransportState {
// reduce state from new, connecting, connected, disconnected, failed, closed, unknown
// to RTCSctpTransport states connecting, connected, closed
let state = this.#pc.connectionState;
if (state === 'new' || state === 'connecting') {
state = 'connecting';
} else if (state === 'disconnected' || state === 'failed' || state === 'closed') {
state = 'closed';
}
return state;
}
get transport(): RTCDtlsTransport {
return this.#transport;
}
}