|
5 | 5 | const inputVolumeBar = document.getElementById("input-volume");
|
6 | 6 | const volumeIndicators = document.getElementById("volume-indicators");
|
7 | 7 | const callButton = document.getElementById("button-call");
|
8 |
| - const hangupButton = document.getElementById("button-hangup"); |
| 8 | + const outgoingCallHangupButton = document.getElementById("button-hangup-outgoing"); |
9 | 9 | const callControlsDiv = document.getElementById("call-controls");
|
10 | 10 | const audioSelectionDiv = document.getElementById("output-selection");
|
11 | 11 | const getAudioDevicesButton = document.getElementById("get-devices");
|
|
36 | 36 | getAudioDevicesButton.onclick = getAudioDevices;
|
37 | 37 | speakerDevices.addEventListener("change", updateOutputDevice);
|
38 | 38 | ringtoneDevices.addEventListener("change", updateRingtoneDevice);
|
39 |
| - hangupButton.onclick = hangup; |
40 |
| - |
| 39 | + |
41 | 40 | // SETUP STEP 1:
|
42 | 41 | // Browser client should be started after a user gesture
|
43 | 42 | // to avoid errors in the browser console re: AudioContext
|
|
105 | 104 | async function makeOutgoingCall() {
|
106 | 105 | var params = {
|
107 | 106 | // get the phone number to call from the DOM
|
108 |
| - phone: phoneNumberInput.value, |
| 107 | + To: phoneNumberInput.value, |
109 | 108 | };
|
110 | 109 |
|
111 | 110 | if (device) {
|
112 |
| - log(`Attempting to call ${params.phone} ...`); |
| 111 | + log(`Attempting to call ${params.To} ...`); |
113 | 112 |
|
114 | 113 | // Twilio.Device.connect() returns a Call object
|
115 | 114 | const call = await device.connect({ params });
|
116 | 115 |
|
117 | 116 | // add listeners to the Call
|
118 | 117 | // "accepted" means the call has finished connecting and the state is now "open"
|
119 |
| - call.addListener("accept", updateUIAcceptedCall); |
120 |
| - call.addListener("disconnect", updateUIDisconnectedCall); |
| 118 | + call.on("accept", updateUIAcceptedOutgoingCall); |
| 119 | + call.on("disconnect", updateUIDisconnectedOutgoingCall); |
| 120 | + call.on("cancel", updateUIDisconnectedOutgoingCall); |
| 121 | + call.on("reject", updateUIDisconnectedOutgoingCall); |
| 122 | + |
| 123 | + outgoingCallHangupButton.onclick = () => { |
| 124 | + log("Hanging up ..."); |
| 125 | + call.disconnect(); |
| 126 | + }; |
| 127 | + |
121 | 128 | } else {
|
122 | 129 | log("Unable to make call.");
|
123 | 130 | }
|
124 | 131 | }
|
125 | 132 |
|
126 |
| - function updateUIAcceptedCall(call) { |
| 133 | + function updateUIAcceptedOutgoingCall(call) { |
127 | 134 | log("Call in progress ...");
|
128 | 135 | callButton.disabled = true;
|
129 |
| - hangupButton.classList.remove("hide"); |
| 136 | + outgoingCallHangupButton.classList.remove("hide"); |
130 | 137 | volumeIndicators.classList.remove("hide");
|
131 | 138 | bindVolumeIndicators(call);
|
132 | 139 | }
|
133 | 140 |
|
134 |
| - function updateUIDisconnectedCall() { |
| 141 | + function updateUIDisconnectedOutgoingCall() { |
135 | 142 | log("Call disconnected.");
|
136 | 143 | callButton.disabled = false;
|
137 |
| - hangupButton.classList.add("hide"); |
| 144 | + outgoingCallHangupButton.classList.add("hide"); |
138 | 145 | volumeIndicators.classList.add("hide");
|
139 | 146 | }
|
140 | 147 |
|
141 |
| - // HANG UP A CALL |
142 |
| - |
143 |
| - function hangup() { |
144 |
| - log("Hanging up ..."); |
145 |
| - if (device) { |
146 |
| - device.disconnectAll(); |
147 |
| - } |
148 |
| - } |
149 |
| - |
150 | 148 | // HANDLE INCOMING CALL
|
151 | 149 |
|
152 | 150 | function handleIncomingCall(call) {
|
|
170 | 168 | };
|
171 | 169 |
|
172 | 170 | // add event listener to call object
|
173 |
| - call.addListener("cancel", handleDisconnectedIncomingCall); |
| 171 | + call.on("cancel", handleDisconnectedIncomingCall); |
| 172 | + call.on("disconnect", handleDisconnectedIncomingCall); |
| 173 | + call.on("reject", handleDisconnectedIncomingCall); |
174 | 174 | }
|
175 | 175 |
|
176 | 176 | // ACCEPT INCOMING CALL
|
|
189 | 189 |
|
190 | 190 | function rejectIncomingCall(call) {
|
191 | 191 | call.reject();
|
192 |
| - |
193 | 192 | log("Rejected incoming call");
|
194 | 193 | resetIncomingCallUI();
|
195 | 194 | }
|
196 | 195 |
|
197 | 196 | // HANG UP INCOMING CALL
|
198 | 197 |
|
199 |
| - function hangupIncomingCall() { |
200 |
| - hangup(); |
201 |
| - |
| 198 | + function hangupIncomingCall(call) { |
| 199 | + call.disconnect(); |
| 200 | + log("Hanging up incoming call"); |
202 | 201 | resetIncomingCallUI();
|
203 | 202 | }
|
204 | 203 |
|
205 | 204 | // HANDLE CANCELLED INCOMING CALL
|
206 | 205 |
|
207 |
| - function handleDisconnectedIncomingCall(call) { |
208 |
| - device.disconnectAll(); |
209 |
| - console.log("handledisconnected : ", call); |
| 206 | + function handleDisconnectedIncomingCall() { |
210 | 207 | log("Incoming call ended.");
|
211 | 208 | resetIncomingCallUI();
|
212 | 209 | }
|
|
226 | 223 |
|
227 | 224 | function resetIncomingCallUI() {
|
228 | 225 | incomingPhoneNumberEl.innerHTML = "";
|
| 226 | + incomingCallAcceptButton.classList.remove("hide"); |
| 227 | + incomingCallRejectButton.classList.remove("hide"); |
| 228 | + incomingCallHangupButton.classList.add("hide"); |
229 | 229 | incomingCallDiv.classList.add("hide");
|
230 | 230 | }
|
231 | 231 |
|
|
0 commit comments