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
|
import type { UnregisterHandler } from "../channel/messages/handler";
import type {
RequestIceCandidateMessage,
ShareIceCandidateMessage,
} from "../channel/messages/messages";
export enum TransferType {
OFFER,
ANSWER,
}
export type Transfer = {
pc: RTCPeerConnection;
channel: RTCDataChannel;
type: TransferType;
};
const servers = {
iceServers: [
{
urls: [
"stun:stun1.l.google.com:19302",
"stun:stun2.l.google.com:19302",
],
},
],
iceCandidatePoolSize: 10,
};
export function createTransfer(
type: TransferType,
onChannel: (channel: RTCDataChannel) => void
): Transfer {
const pc = new RTCPeerConnection(servers);
const channel = pc.createDataChannel("channel", {
negotiated: true,
id: 0,
});
channel.onopen = () => onChannel(channel);
return {
pc,
channel,
type,
};
}
export function onIncomingIceCandidate(
transfer: Transfer,
message: ShareIceCandidateMessage | RequestIceCandidateMessage
) {
transfer.pc.addIceCandidate(message.candidate);
}
export function unregisterIceOnComplete(
transfer: Transfer,
unregister: UnregisterHandler
) {
transfer.pc.onicegatheringstatechange = event => {
const connection = event.target as any;
if (connection.iceGatheringState === "complete") {
unregister();
}
};
}
|