-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalling.js
64 lines (51 loc) · 1.87 KB
/
calling.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
import { CallClient } from "@azure/communication-calling";
import { AzureCommunicationTokenCredential } from '@azure/communication-common';
const userToken = document.getElementById("token-input");
const submitToken = document.getElementById("token-submit");
const calleeInput = document.getElementById("callee-id-input");
const callButton = document.getElementById("call-button");
const hangUpButton = document.getElementById("hang-up-button");
let call;
let callAgent;
let tokenCredential;
submitToken.addEventListener("click", async () => {
const callClient = new CallClient();
const userTokenCredential = userToken.value;
try {
tokenCredential = new AzureCommunicationTokenCredential(userTokenCredential);
callAgent = await callClient.createCallAgent(tokenCredential);
callAgent.on('incomingCall', incomingCallHandler);
callButton.disabled = false;
submitToken.disabled = true;
} catch(error) {
window.alert("Please submit a valid token!");
}
})
callButton.addEventListener("click", () => {
// start a call
const userToCall = calleeInput.value;
call = callAgent.startCall(
[{ id: userToCall }],
{}
);
// toggle button states
hangUpButton.disabled = false;
callButton.disabled = true;
});
hangUpButton.addEventListener("click", () => {
// end the current call
call.hangUp({ forEveryone: true });
// toggle button states
hangUpButton.disabled = true;
callButton.disabled = false;
});
const incomingCallHandler = async (args) => {
const incomingCall = args.incomingCall;
console.log("CALL ACCEPTED")
// Accept the call
await incomingCall.accept();
// Subscribe to callEnded event and get the call end reason
incomingCall.on('callEnded', args => {
console.log(args.callEndReason);
});
};