-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
141 lines (117 loc) · 4.22 KB
/
app.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
let socket;
const becomeHost = () => new Promise((resolve, reject) => {
socket.onmessage = (msg) => {
const data = JSON.parse(msg.data);
if (data.type === 'HostResponse') {
resolve(data.success);
} else {
reject();
}
};
socket.send(JSON.stringify({
type: "HostRequest"
}));
});
socket = new WebSocket(`ws://${window.location.hostname}`);
const connections = {};
const channels = {};
const outputDiv = document.getElementById('output');
const listenForConnections = () => {
socket.onmessage = (msg) => {
const data = JSON.parse(msg.data);
if (data.type === 'PeerRequest') {
const thing = new RTCPeerConnection();
const dataChannel = thing.createDataChannel('homegames');
thing.onicecandidate = (e) => {
if (e.candidate !== null) {
const offerMessage = {
type: "RTCOffer",
targetId: data.id,
offer: thing.localDescription
};
socket.send(JSON.stringify(offerMessage));
}
};
thing.ondatachannel = (e) => {
const chan = e.channel || e;
channels[data.id] = chan;
};
dataChannel.onmessage = (msg) => {
console.log("got a message from a peer?");
console.log(msg);
};
connections[data.id] = thing;
thing.createOffer().then((offer) => {
thing.setLocalDescription(offer);
});
} else if (data.type === 'answer') {
const connection = connections[data.targetId];
connection.setRemoteDescription(new RTCSessionDescription(data.answer));
}
};
};
const makePeerRequest = () => {
socket.onmessage = (msg) => {
const data = JSON.parse(msg.data);
if (data.type === 'offer') {
const connection = new RTCPeerConnection();
const dataChannel = connection.createDataChannel('homegames');
connection.onicecandidate = (e) => {
if (e.candidate !== null) {
socket.send(JSON.stringify(connection.localDescription));
}
};
connection.ondatachannel = (e) => {
const chan = e.channel || e;
if (chan && chan.readyState === 'open') {
chan.send("AYY LMAO I AM A PEER");
}
};
const times = [];
dataChannel.onmessage = (msg) => {
const diff = Date.now() - Number(msg.data);
times.push(Date.now());
if (times.length % 60 === 0) {
output.innerHTML = 'Got 60 frames in ' + Number(times[times.length - 1] - times[times.length - 61]) + 'ms. ' + times.length + ' total messages in ' + Number(times[times.length - 1] - times[0]) + 'ms';
}
};
connection.setRemoteDescription(new RTCSessionDescription(data));
connection.createAnswer().then((answer) => {
connection.setLocalDescription(answer);
}).then(_ => {
console.log("WHAT IS THIS");
if (connection.canTrickleIceCandidates) {
console.log("WHAT");
return connection.localDescription;
}
}).then(ting => {
console.log("TING?");
console.log(ting);
});
}
};
socket.send(JSON.stringify({
type: "PeerRequest"
}));
};
const broadcastTimestamps = () => {
const timestamp = Date.now();
for (const clientId in channels) {
const channel = channels[clientId];
if (channel.readyState === 'open') {
channel.send(timestamp);
}
}
};
socket.onopen = async () => {
const isHost = await becomeHost();
if (isHost) {
listenForConnections();
setInterval(broadcastTimestamps, 2);
} else {
makePeerRequest();
}
};
onerror = (a, b, c, d, e, f) => {
errors.innerHTML = '' + a + ' ' + b + ' ' + c + ' ' + d + ' ' + e + ' ' + f;
};