-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (91 loc) · 2.81 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
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
const socket = io(window.location.origin);
let selectedUser;
const onUpdateUserList = ({ userIds }) => {
const usersList = document.querySelector('#usersList');
const usersToDisplay = userIds.filter(id => id !== socket.id);
usersList.innerHTML = '';
usersToDisplay.forEach(user => {
const userItem = document.createElement('div');
userItem.innerHTML = user;
userItem.className = 'user-item';
userItem.addEventListener('click', () => {
selectedUser = user;
});
usersList.appendChild(userItem);
});
};
socket.on('update-user-list', onUpdateUserList);
const createPeerConnection = () => {
return new RTCPeerConnection({
iceServers: [
{
urls: "stun:stun.stunprotocol.org"
}
]
});
};
const peer = createPeerConnection();
const onSocketConnect = async () => {
document.querySelector('#userId').innerHTML = `My user id is ${socket.id}`
const constraints = {
audio: true,
video: true
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
document.querySelector('#localVideo').srcObject = stream;
stream.getTracks().forEach(track => peer.addTrack(track, stream));
callButton.disabled = false;
};
socket.on('connect', onSocketConnect);
const call = async () => {
callButton.disabled = true;
const localPeerOffer = await peer.createOffer();
await peer.setLocalDescription(new RTCSessionDescription(localPeerOffer));
socket.emit('mediaOffer', {
offer: localPeerOffer,
from: socket.id,
to: selectedUser
})
};
const callButton = document.querySelector('#call');
callButton.addEventListener('click', call);
const onMediaOffer = async (data) => {
try {
await peer.setRemoteDescription(new RTCSessionDescription(data.offer));
const peerAnswer = await peer.createAnswer();
await peer.setLocalDescription(new RTCSessionDescription(peerAnswer));
socket.emit('mediaAnswer', {
answer: peerAnswer,
from: socket.id,
to: data.from
});
} catch (error) {
// Handle error
}
};
socket.on('mediaOffer', onMediaOffer);
const onMediaAnswer = async data => {
await peer.setRemoteDescription(new RTCSessionDescription(data.answer));
};
socket.on('mediaAnswer', onMediaAnswer);
const onIceCandidateEvent = event => {
socket.emit('iceCandidate', {
to: selectedUser,
candidate: event.candidate
});
};
peer.onicecandidate = onIceCandidateEvent;
const onRemotePeerIceCandidate = async data => {
try {
const candidate = new RTCIceCandidate(data.candidate);
await peer.addIceCandidate(candidate);
} catch (error) {
// Handle error
}
};
socket.on('remotePeerIceCandidate', onRemotePeerIceCandidate)
const gotRemoteStream = event => {
const [stream] = event.streams;
document.querySelector('#remoteVideo').srcObject = stream;
};
peer.addEventListener('track', gotRemoteStream);