generated from akshatvg/Agora-Change-Remote-User-Role
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
354 lines (329 loc) · 12.6 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// create Agora client
var client = AgoraRTC.createClient({ mode: "live", codec: "vp8" });
// RTM Global Vars
var isLoggedIn = false;
var localTracks = {
videoTrack: null,
audioTrack: null
};
var localTrackState = {
videoTrackEnabled: true,
audioTrackEnabled: true
}
// Hand raise state
var handRaiseState = false;
var remoteUsers = {};
// Agora client options
var options = {
appid: $("#appid").val(),
channel: null,
uid: null,
token: null,
accountName: null,
role: "audience"
};
// Host join
$("#host-join").click(function (e) {
RTMJoin();
options.role = "host";
})
// Audience join
$("#audience-join").click(function (e) {
RTMJoin();
options.role = "audience";
})
// Join form submission
$("#join-form").submit(async function (e) {
e.preventDefault();
$("#host-join").attr("disabled", true);
$("#audience-join").attr("disabled", true);
try {
options.appid = $("#appid").val();
options.token = $("#token").val();
options.channel = $("#channel").val();
options.accountName = $('#accountName').val();
await join();
} catch (error) {
console.error(error);
} finally {
$("#raise-hand").attr("disabled", false);
$("#leave").attr("disabled", false);
}
})
// Leave click
$("#leave").click(function (e) {
leave();
})
async function join() { // create Agora client
client.setClientRole(options.role);
$("#mic-btn").prop("disabled", false);
$("#video-btn").prop("disabled", false);
if (options.role === "audience") {
$("#mic-btn").prop("disabled", true);
$("#video-btn").prop("disabled", true);
$("#raise-hand-div").append(`<button id="raise-hand" type="button" class="btn btn-live btn-sm" disabled>Raise Hand</button>`);
// Event listeners
client.on("user-published", handleUserPublished);
client.on("user-joined", handleUserJoined);
client.on("user-left", handleUserLeft);
}
// join the channel
options.uid = await client.join(options.appid, options.channel, options.token || null);
if (options.role === "host") {
$('#mic-btn').prop('disabled', false);
$('#video-btn').prop('disabled', false);
client.on("user-published", handleUserPublished);
client.on("user-joined", handleUserJoined);
client.on("user-left", handleUserLeft);
// create local audio and video tracks
localTracks.audioTrack = await AgoraRTC.createMicrophoneAudioTrack();
localTracks.videoTrack = await AgoraRTC.createCameraVideoTrack();
// play local video track
localTracks.videoTrack.play("local-player");
$("#local-player-name").text(`localTrack(${options.uid
})`);
// publish local tracks to channel
await client.publish(Object.values(localTracks));
console.log("Successfully published.");
}
}
// Leave
async function leave() {
if (options.role === "audience") {
$("#raise-hand-div").empty();
}
for (trackName in localTracks) {
var track = localTracks[trackName];
if (track) {
track.stop();
track.close();
$('#mic-btn').prop('disabled', true);
$('#video-btn').prop('disabled', true);
localTracks[trackName] = undefined;
}
}
// remove remote users and player views
remoteUsers = {};
$("#remote-playerlist").html("");
// leave the channel
await client.leave();
$("#local-player-name").text("");
$("#host-join").attr("disabled", false);
$("#audience-join").attr("disabled", false);
$("#leave").attr("disabled", true);
$("#raise-hand").attr("disabled", true);
console.log("Client successfully left channel.");
}
async function RTMJoin() { // Create Agora RTM client
const clientRTM = AgoraRTM.createInstance($("#appid").val(), { enableLogUpload: false });
var accountName = $('#accountName').val();
// Login
clientRTM.login({ uid: accountName }).then(() => {
console.log('AgoraRTM client login success. Username: ' + accountName);
isLoggedIn = true;
// RTM Channel Join
var channelName = $('#channel').val();
channel = clientRTM.createChannel(channelName);
channel.join().then(() => {
console.log('AgoraRTM client channel join success.');
// Send channel message for raising hand
$(document).on('click', '#raise-hand', async function () {
fullDivId = $(this).attr('id');
if (handRaiseState === false) {
$("#raise-hand").text("Lower Hand");
handRaiseState = true;
console.log("Hand raised.");
// Inform channel that rand was raised
await channel.sendMessage({ text: "raised" }).then(() => {
console.log("Message sent successfully.");
console.log("Your message was: raised" + " sent by: " + accountName);
}).catch((err) => {
console.error("Message sending failed: " + err);
})
}
else if (handRaiseState === true) {
$("#raise-hand").text("Raise Hand");
handRaiseState = false;
console.log("Hand lowered.");
// Inform channel that rand was raised
await channel.sendMessage({ text: "lowered" }).then(() => {
console.log("Message sent successfully.");
console.log("Your message was: lowered" + " sent by: " + accountName);
}).catch((err) => {
console.error("Message sending failed: " + err);
})
}
});
// Get channel message when someone raises hand
channel.on('ChannelMessage', async function (text, peerId) {
console.log(peerId + " changed their hand raise state to " + text.text);
if (options.role === "host") {
if (text.text == "raised") {
// Ask host if user who raised their hand should be called onto stage or not
$('#confirm').modal('show');
$('#modal-body').text(peerId + " raised their hand. Do you want to make them a host?");
$('#promoteAccept').click(async function () {
// Call user onto stage
console.log("The host accepted " + peerId + "'s request.");
await clientRTM.sendMessageToPeer({
text: "host"
},
peerId,
).then(sendResult => {
if (sendResult.hasPeerReceived) {
console.log("Message has been received by: " + peerId + " Message: host");
} else {
console.log("Message sent to: " + peerId + " Message: host");
}
}).catch(error => {
console.log("Error sending peer message: " + error);
});
$('#confirm').modal('hide');
});
$("#cancel").click(async function () {
// Inform the user that they were not made a host
console.log("The host rejected " + peerId + "'s request.");
await clientRTM.sendMessageToPeer({
text: "audience"
},
peerId,
).then(sendResult => {
if (sendResult.hasPeerReceived) {
console.log("Message has been received by: " + peerId + " Message: audience");
} else {
console.log("Message sent to: " + peerId + " Message: audience");
}
}).catch((error) => {
console.log("Error sending peer message: " + error);
});
$('#confirm').modal('hide');
});
} else if (text.text == "lowered") {
$('#confirm').modal('hide');
console.log("Hand lowered so host can ignore this.");
}
}
})
// Display messages from host when they approve the request
clientRTM.on('MessageFromPeer', async function ({
text
}, peerId) {
console.log(peerId + " changed your role to " + text);
if (text === "host") {
await leave();
options.role = "host";
console.log("Role changed to host.");
await client.setClientRole("host");
await join();
$("#host-join").attr("disabled", true);
$("#audience-join").attr("disabled", true);
$("#raise-hand").attr("disabled", false);
$("#leave").attr("disabled", false);
} else if (text === "audience" && options.role !== "audience") {
alert("The host rejected your proposal to be called onto stage.");
$("#raise-hand").attr("disabled", false);
}
})
}).catch(error => {
console.log('AgoraRTM client channel join failed: ', error);
}).catch(err => {
console.log('AgoraRTM client login failure: ', err);
});
});
// Logout
document.getElementById("leave").onclick = async function () {
console.log("Client logged out of RTM.");
await clientRTM.logout();
}
}
// Subscribe to a remote user
async function subscribe(user, mediaType) {
const uid = user.uid;
await client.subscribe(user, mediaType);
console.log("Successfully subscribed.");
if (mediaType === 'video') {
const player = $(`
<div id="player-wrapper-${uid}">
<p class="player-name">remoteUser(${uid})</p>
<div id="player-${uid}" class="player"></div>
</div>
`);
$("#remote-playerlist").append(player);
user.videoTrack.play(`player-${uid}`);
}
if (mediaType === 'audio') {
user.audioTrack.play();
}
}
// Handle user published
function handleUserPublished(user, mediaType) {
const id = user.uid;
remoteUsers[id] = user;
subscribe(user, mediaType);
}
// Handle user joined
function handleUserJoined(user, mediaType) {
const id = user.uid;
remoteUsers[id] = user;
subscribe(user, mediaType);
}
// Handle user left
function handleUserLeft(user) {
const id = user.uid;
delete remoteUsers[id];
$(`#player-wrapper-${id}`).remove();
}
// Mute audio click
$("#mic-btn").click(function (e) {
if (localTrackState.audioTrackEnabled) {
muteAudio();
} else {
unmuteAudio();
}
});
// Mute video click
$("#video-btn").click(function (e) {
if (localTrackState.videoTrackEnabled) {
muteVideo();
} else {
unmuteVideo();
}
})
// Hide mute buttons
function hideMuteButton() {
$("#video-btn").css("display", "none");
$("#mic-btn").css("display", "none");
}
// Show mute buttons
function showMuteButton() {
$("#video-btn").css("display", "inline-block");
$("#mic-btn").css("display", "inline-block");
}
// Mute audio function
async function muteAudio() {
if (!localTracks.audioTrack) return;
await localTracks.audioTrack.setEnabled(false);
localTrackState.audioTrackEnabled = false;
$("#mic-btn").text("Unmute Audio");
}
// Mute video function
async function muteVideo() {
if (!localTracks.videoTrack) return;
await localTracks.videoTrack.setEnabled(false);
localTrackState.videoTrackEnabled = false;
$("#video-btn").text("Unmute Video");
}
// Unmute audio function
async function unmuteAudio() {
if (!localTracks.audioTrack) return;
await localTracks.audioTrack.setEnabled(true);
localTrackState.audioTrackEnabled = true;
$("#mic-btn").text("Mute Audio");
}
// Unmute video function
async function unmuteVideo() {
if (!localTracks.videoTrack) return;
await localTracks.videoTrack.setEnabled(true);
localTrackState.videoTrackEnabled = true;
$("#video-btn").text("Mute Video");
}