forked from cjb/serverless-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
62 lines (58 loc) · 2.26 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>WebRTC chat</title>
</head>
<body>
<h1>Open the JS console!</h1>
<script>
var RTCPeerConnection = window.RTCPeerConnection || webkitRTCPeerConnection || mozRTCPeerConnection;
var peerConn = new RTCPeerConnection({'iceServers': [{'urls': ['stun:stun.l.google.com:19302']}]});
console.log('Call create(), or join("some offer")');
function create() {
console.log("Creating ...");
var dataChannel = peerConn.createDataChannel('test');
dataChannel.onopen = (e) => {
window.say = (msg) => { dataChannel.send(msg); };
console.log('Say things with say("hi")');
};
dataChannel.onmessage = (e) => { console.log('Got message:', e.data); };
peerConn.createOffer({})
.then((desc) => peerConn.setLocalDescription(desc))
.then(() => {})
.catch((err) => console.error(err));
peerConn.onicecandidate = (e) => {
if (e.candidate == null) {
console.log("Get joiners to call: ", "join(", JSON.stringify(peerConn.localDescription), ")");
}
};
window.gotAnswer = (answer) => {
console.log("Initializing ...");
peerConn.setRemoteDescription(new RTCSessionDescription(answer));
};
}
function join(offer) {
console.log("Joining ...");
peerConn.ondatachannel = (e) => {
var dataChannel = e.channel;
dataChannel.onopen = (e) => {
window.say = (msg) => { dataChannel.send(msg); };
console.log('Say things with say("hi")');
};
dataChannel.onmessage = (e) => { console.log('Got message:', e.data); }
};
peerConn.onicecandidate = (e) => {
if (e.candidate == null) {
console.log("Get the creator to call: gotAnswer(", JSON.stringify(peerConn.localDescription), ")");
}
};
var offerDesc = new RTCSessionDescription(offer);
peerConn.setRemoteDescription(offerDesc);
peerConn.createAnswer({})
.then((answerDesc) => peerConn.setLocalDescription(answerDesc))
.catch((err) => console.warn("Couldn't create answer"));
}
</script>
</body>
</html>