Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit 66210e7

Browse files
authored
Fix display bug, add event listeners to calls (#24)
1 parent a4f459a commit 66210e7

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ <h2 class="instructions">Make a Call</h2>
3232
<input id="phone-number" type="text" placeholder="+15552221234" />
3333
<button id="button-call" type="submit">Call</button>
3434
</form>
35-
<button id="button-hangup" class="hide">Hang Up</button>
35+
<button id="button-hangup-outgoing" class="hide">Hang Up</button>
3636
<div id="incoming-call" class="hide">
3737
<h2>Incoming Call Controls</h2>
3838
<p class="instructions">

public/quickstart.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const inputVolumeBar = document.getElementById("input-volume");
66
const volumeIndicators = document.getElementById("volume-indicators");
77
const callButton = document.getElementById("button-call");
8-
const hangupButton = document.getElementById("button-hangup");
8+
const outgoingCallHangupButton = document.getElementById("button-hangup-outgoing");
99
const callControlsDiv = document.getElementById("call-controls");
1010
const audioSelectionDiv = document.getElementById("output-selection");
1111
const getAudioDevicesButton = document.getElementById("get-devices");
@@ -36,7 +36,7 @@
3636
getAudioDevicesButton.onclick = getAudioDevices;
3737
speakerDevices.addEventListener("change", updateOutputDevice);
3838
ringtoneDevices.addEventListener("change", updateRingtoneDevice);
39-
hangupButton.onclick = hangup;
39+
4040

4141
// SETUP STEP 1:
4242
// Browser client should be started after a user gesture
@@ -116,37 +116,36 @@
116116

117117
// add listeners to the Call
118118
// "accepted" means the call has finished connecting and the state is now "open"
119-
call.addListener("accept", updateUIAcceptedCall);
120-
call.addListener("disconnect", updateUIDisconnectedCall);
119+
call.on("accept", updateUIAcceptedOutgoingCall);
120+
call.on("disconnect", updateUIDisconnectedOutgoingCall);
121+
call.on("cancel", updateUIDisconnectedOutgoingCall);
122+
call.on("reject", updateUIDisconnectedOutgoingCall);
123+
124+
outgoingCallHangupButton.onclick = () => {
125+
log("Hanging up ...");
126+
call.disconnect();
127+
};
128+
121129
} else {
122130
log("Unable to make call.");
123131
}
124132
}
125133

126-
function updateUIAcceptedCall(call) {
134+
function updateUIAcceptedOutgoingCall(call) {
127135
log("Call in progress ...");
128136
callButton.disabled = true;
129-
hangupButton.classList.remove("hide");
137+
outgoingCallHangupButton.classList.remove("hide");
130138
volumeIndicators.classList.remove("hide");
131139
bindVolumeIndicators(call);
132140
}
133141

134-
function updateUIDisconnectedCall() {
142+
function updateUIDisconnectedOutgoingCall() {
135143
log("Call disconnected.");
136144
callButton.disabled = false;
137-
hangupButton.classList.add("hide");
145+
outgoingCallHangupButton.classList.add("hide");
138146
volumeIndicators.classList.add("hide");
139147
}
140148

141-
// HANG UP A CALL
142-
143-
function hangup() {
144-
log("Hanging up ...");
145-
if (device) {
146-
device.disconnectAll();
147-
}
148-
}
149-
150149
// HANDLE INCOMING CALL
151150

152151
function handleIncomingCall(call) {
@@ -170,7 +169,9 @@
170169
};
171170

172171
// add event listener to call object
173-
call.addListener("cancel", handleDisconnectedIncomingCall);
172+
call.on("cancel", handleDisconnectedIncomingCall);
173+
call.on("disconnect", handleDisconnectedIncomingCall);
174+
call.on("reject", handleDisconnectedIncomingCall);
174175
}
175176

176177
// ACCEPT INCOMING CALL
@@ -189,23 +190,21 @@
189190

190191
function rejectIncomingCall(call) {
191192
call.reject();
192-
193193
log("Rejected incoming call");
194194
resetIncomingCallUI();
195195
}
196196

197197
// HANG UP INCOMING CALL
198198

199-
function hangupIncomingCall() {
200-
hangup();
201-
199+
function hangupIncomingCall(call) {
200+
call.disconnect();
201+
log("Hanging up incoming call");
202202
resetIncomingCallUI();
203203
}
204204

205205
// HANDLE CANCELLED INCOMING CALL
206206

207-
function handleDisconnectedIncomingCall(call) {
208-
device.disconnectAll();
207+
function handleDisconnectedIncomingCall() {
209208
log("Incoming call ended.");
210209
resetIncomingCallUI();
211210
}
@@ -225,6 +224,9 @@
225224

226225
function resetIncomingCallUI() {
227226
incomingPhoneNumberEl.innerHTML = "";
227+
incomingCallAcceptButton.classList.remove("hide");
228+
incomingCallRejectButton.classList.remove("hide");
229+
incomingCallHangupButton.classList.add("hide");
228230
incomingCallDiv.classList.add("hide");
229231
}
230232

src/handler.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ exports.tokenGenerator = function tokenGenerator() {
3131

3232
exports.voiceResponse = function voiceResponse(requestBody) {
3333
const toNumberOrClientName = requestBody.To;
34-
34+
const callerId = config.callerId;
3535
let twiml = new VoiceResponse();
3636

3737
// If the request to the /voice endpoint is TO your Twilio Number,
3838
// then it is an incoming call towards your Twilio.Device.
39-
if (requestBody.To == config.callerId) {
40-
41-
dial = twiml.dial();
39+
if (toNumberOrClientName == callerId) {
40+
let dial = twiml.dial();
4241

4342
// This will connect the caller with your Twilio.Device/client
4443
dial.client(identity);
@@ -47,7 +46,7 @@ exports.voiceResponse = function voiceResponse(requestBody) {
4746
// This is an outgoing call
4847

4948
// set the callerId
50-
dial = twiml.dial({ callerId: config.callerId });
49+
let dial = twiml.dial({ callerId });
5150

5251
// Check if the 'To' parameter is a Phone Number or Client Name
5352
// in order to use the appropriate TwiML noun

0 commit comments

Comments
 (0)