-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
93 lines (80 loc) · 2.84 KB
/
Copy pathserver.js
File metadata and controls
93 lines (80 loc) · 2.84 KB
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
var nid;
var urltxt;
var fid;
function shareURL() {
let shareData = {
title: "test client URL",
text: "Please click this link to test setup time",
url: urltxt,
}
if (!navigator.canShare) {
console.log("navigator.canShare() not supported.");
} else if (navigator.canShare(shareData)) {
console.log("navigator.canShare() supported. We can use navigator.share() to send the data.");
navigator.share(shareData).catch((err) => {
console.log("cant share because " + err)
});
} else {
console.log("Specified data cannot be shared.");
}
console.log("share url is " + urltxt);
}
function startUX() {
nid = mid;
$("#role").text("Server");
$("#status").text("Connected");
urltxt = window.location.href.replace("server.html", "client.html") + "?id=" + mid;
let p1 = document.getElementById("P1");
if (!navigator.canShare) {
console.log("navigator.canShare() not supported.");
p1.innerText = "Share this link with your guests. " + urltxt
} else {
p1.innerHTML = "Click <button onclick='shareURL()' id='shareURL' class='btn btn-danger'>Share</button> to send link to guests. "
shared();
}
}
function shared() {
pc.ondatachannel = (e) => {
let tick = Date.now();
pc.createDataChannel(""+tick);
$("#status").text("client created datachannel " + e.channel.label);
}
console.log("ready for offer");
$("#status").text("Waiting for guests.");
}
function messageDeal(event) {
let lines = event.data.split("\n");
lines.forEach((line) => {
var data = JSON.parse(line);
console.log("message data is ", line);
if (data.to != nid) {
alert("message mixup");
}
switch (data.type) {
case "offer":
fid = data.from;
pc.setRemoteDescription(data)
.then(_ => {
pc.createAnswer().then(ans => {
pc.setLocalDescription(ans).then(_ =>
sendMessage(fid, nid, "answer", ans.sdp)
)
});
$("#status").text("Trying to connect call");
})
.catch(e => console.log("set Remote answer error", e));
break;
case "candidate":
var jc = {
sdpMLineIndex: 0,
candidate: data.sdp
};
console.log("adding candidate ", jc);
var nc = new RTCIceCandidate(jc);
pc.addIceCandidate(nc)
.then(_ => console.log("added remote candidate"))
.catch((e) => console.log("couldn't add candidate ", e));
break;
}
});
}