Skip to content

Commit

Permalink
Merge pull request #70 from signalwire/joao/ws_incoming_calls
Browse files Browse the repository at this point in the history
Handle incoming calls
  • Loading branch information
jpsantosbh authored Mar 13, 2024
2 parents 39191f0 + 3391a31 commit bc0bf30
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
43 changes: 35 additions & 8 deletions public/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ async function enablePushNotifications() {
deviceType: 'Android', // Use Android w/ Firebase on the web
deviceToken: token,
})
client.online({pushNotification: __incomingCallNotification})
pnSecretKey = push_notification_key
console.log('pnSecretKey: ', pnSecretKey)
btnRegister.classList.add('d-none')
Expand All @@ -165,11 +166,7 @@ async function handlePushNotification(pushNotificationPayload) {

switch (resultType) {
case 'inboundCall':
window.__call = resultObject
window.__call.on('destroy', () => {
console.warn('Inbound Call got cancelled!!')
})
updateUIRinging()
this.logger.info('Inbound Call Push Notification received')
break
default:
this.logger.warn('Unknown resultType', resultType, resultObject)
Expand Down Expand Up @@ -367,7 +364,7 @@ async function getClient() {
client = await SWire({
host: !!_host && _host.trim().length ? _host : undefined,
token: _token,
rootElement: document.getElementById('rootElement'),
// rootElement: document.getElementById('rootElement'),
})
}

Expand Down Expand Up @@ -402,6 +399,7 @@ window.connect = async () => {
logLevel: 'debug',
debug: { logWsTraffic: true },
nodeId: steeringId(),
rootElement: document.getElementById('rootElement')
})

window.__call = call
Expand Down Expand Up @@ -550,13 +548,42 @@ function updateUIConnected() {
})
}

window.__avaliable = false

window.toggleAvaliable = async () => {
window.__avaliable = ! window.__avaliable
const isOn = window.__avaliable
btnAvaliable.innerText = isOn ? 'get offline' : 'get online'
btnAvaliable.classList = isOn ? 'btn btn-success' : 'btn btn-warning'
if(!window.__client) {
window.__client = await getClient()
}

if(isOn) {
window.__client.online({all: __incomingCallNotification})
} else {
window.__client.offline()
}
}

window.__incomingCallNotification = (notification) => {
if(!window.__invite || window.__invite.details.callID !== notification.invite.details.callID) {
window.__invite = notification.invite
}
updateUIRinging()
}

window.answer = async () => {
await window.__call.answer()
const call = await window.__invite.accept({rootElement: document.getElementById('rootElement')})
window.__call = call
window.__call.on('destroy', () => {
console.warn('Inbound Call got cancelled!!')
})
updateUIConnected()
}

window.reject = async () => {
await window.__call.hangup()
await window.__invite.reject()
restoreUI()
}
/**
Expand Down
7 changes: 5 additions & 2 deletions views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</div>
<div class="d-grid gap-2 mt-2">
<button id="btnConnect" class="btn btn-success" onclick="connect()">
Connect
Dial
</button>
<button id="btnAnswer" class="btn btn-warning d-none" onclick="answer()">
Answer
Expand All @@ -115,8 +115,11 @@
<div class="card mt-2">
<div class="d-grid gap-2">
<a id="btnRegister" class="btn btn-success" href="/?inbound">
Register
Register Device for Push Notification
</a>
<button id="btnAvaliable" class="btn btn-warning" onclick="toggleAvaliable()">
Avaliable
</button>
</div>
</div>

Expand Down

0 comments on commit bc0bf30

Please sign in to comment.