-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (64 loc) · 1.93 KB
/
index.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
const socket = io('localhost:8008');
const tracker = new SimpleSignalClient(socket);
// Initiate peer connection
async function connectToPeer(id) {
// Don't request connection to self
if (id === socket.id) return;
// log peer and connect
console.log('connecting to peer', id);
const { peer, metadata } = await tracker.connect(id);
console.log('connected to', id, metadata);
onPeer(peer);
}
// Handle peer data
function onPeer (peer) {
// Handle peer connect
peer.on('connect', () => {
tracker.peers().forEach((p, i) => {
// skip self
if (i != 0) {
p.send(JSON.stringify({
type: 'NEWCONNECT',
data: { message: `New connection from peer ${peer._id}` }
}))
}
});
});
peer.on('data', (message) => {
let data = dataToObject(message);
switch (data.type) {
case 'NEWCONNECT':
default:
console.log('peer', peer._id, '\ndata', data);
}
});
console.log('tracker', tracker);
console.log('tracker peers', tracker.peers());
}
// Convert Uint8Array buffer to object
function dataToObject (data) {
return JSON.parse(new TextDecoder('utf-8').decode(data));
}
// On tracker request, accept and handle peer
tracker.on('request', async (request) => {
const { peer, metadata } = await request.accept();
console.log('connected to peer', peer, metadata);
onPeer(peer);
});
// On tracker discovery, connect to peers
tracker.on('discover', (data) => {
console.log('discovery', data);
data.peers.forEach(id => connectToPeer(id));
});
// Get initial peers
console.log('requesting peers');
tracker.discover({
config: {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:global.stun.twilio.com:3478?transport=udp' }
]
},
stream: false,
trickle: true
});