-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconference.js
119 lines (103 loc) · 3.71 KB
/
conference.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
const getPasscode = () => {
const passcodeInput = document.getElementById('passcode') || {};
const passcode = passcodeInput.value;
passcodeInput.value = '';
return passcode;
};
const trackSubscribed = (div, track) => {
div.appendChild(track.attach());
};
const trackUnsubscribed = (track) => {
track.detach().forEach((element) => element.remove());
};
// connect participant
const participantConnected = (participant) => {
console.log(`Participant ${participant.identity} connected'`);
const participantsDiv = document.getElementById('participants');
const div = document.createElement('div'); // create div for new participant
div.id = participant.sid;
participant.on('trackSubscribed', (track) => trackSubscribed(div, track));
participant.on('trackUnsubscribed', trackUnsubscribed);
participant.tracks.forEach((publication) => {
if (publication.isSubscribed) {
trackSubscribed(div, publication.track);
}
});
participantsDiv.appendChild(div);
};
const participantDisconnected = (participant) => {
console.log(`Participant ${participant.identity} disconnected.`);
document.getElementById(participant.sid).remove();
};
(() => {
const { Video } = Twilio;
let videoRoom;
let localStream;
const video = document.getElementById('video');
// preview screen
navigator.mediaDevices
.getUserMedia({ video: true, audio: true })
.then((vid) => {
video.srcObject = vid;
localStream = vid;
});
const joinRoomButton = document.getElementById('button-join');
const leaveRoomButton = document.getElementById('button-leave');
const roomControlsForm = document.getElementById('room-controls-form');
const preConnectControls = document.getElementById('pre-connect-controls');
const postConnectControls = document.getElementById('post-connect-controls');
const participantsDiv = document.getElementById('participants');
const permissionsHelp = document.getElementById('permissions-help');
const joinRoom = (event) => {
event.preventDefault();
// get access token
fetch(`video-token?passcode=${getPasscode()}`)
.then((resp) => {
if (resp.ok) {
return resp.json();
}
console.error(resp);
if (resp.status === 401) {
throw new Error('Invalid passcode');
} else {
throw new Error('Unexpected error. Open dev tools for logs');
}
})
.then((body) => {
const { token, room } = body;
console.log(token);
// connect to room
return Video.connect(token, { name: room });
})
.then((room) => {
console.log(`Connected to Room ${room.name}`);
videoRoom = room;
room.participants.forEach(participantConnected);
room.on('participantConnected', participantConnected);
room.on('participantDisconnected', participantDisconnected);
room.once('disconnected', (error) =>
room.participants.forEach(participantDisconnected)
);
preConnectControls.style.display = 'none';
permissionsHelp.style.display = 'none';
postConnectControls.style.display = 'inline-block';
participantsDiv.style.display = 'flex';
})
.catch((err) => {
// eslint-disable-next-line no-alert
alert(err.message);
});
};
roomControlsForm.onsubmit = joinRoom;
joinRoomButton.onclick = joinRoom;
// leave room
leaveRoomButton.onclick = (event) => {
videoRoom.disconnect();
console.log(`Disconnected from Room ${videoRoom.name}`);
preConnectControls.style.display = 'inline-block';
permissionsHelp.style.display = 'block';
postConnectControls.style.display = 'none';
participantsDiv.style.display = 'none';
event.preventDefault();
};
})();