我正在使用 Azure 通信服务在 2 个人(第 1 个人和第 2 个人)之间创建视频通话。
第一个连接的人(对于示例 1)等待第二个连接(对于示例 2)。 当第二个人接通时,他们发起呼叫。 因此,人员 1 会收到通知,并且必须使用此功能接听电话:
callAgent.on('incomingCall', async (args) => {
try {
//show the button accept call
} catch (error) {
}
});
无人接听则自动呼叫不成功。
我的问题是,当 2 人同时发起呼叫时,2 个人中的一个应答,但另一个已连接到呼叫,无法应答,因此呼叫被断开,就好像没有应答一样。
是否可以在通话开始时销毁传入呼叫?
当双方尝试同时发起呼叫时同时处理来电和活动呼叫的步骤:
incomingCall
事件处理程序来监听来电。如果此人已经在通话中,您可能需要驳回或拒绝来电。args.call.reject()
事件处理程序中使用 incomingCall
拒绝来电。incomingCall
和在 Azure 通信中进行管理。
let activeCall = null;
callAgent.on('incomingCall', async (args) => {
try {
// Check if there is an active call already
if (activeCall) {
// Decline the incoming call automatically if the user is in an active call
await args.call.reject();
console.log('Incoming call declined because the user is in an active call.');
} else {
// Show the button to accept the incoming call
// If accepted, set the call as activeCall
activeCall = await args.accept();
}
} catch (error) {
console.error('Error handling incoming call:', error);
}
});
// Sample code for initiating a call
async function initiateCall() {
if (activeCall) {
console.log('Already in a call, cannot initiate a new call.');
return;
}
// Assuming you have a target user ID or phone number to call
const targetUserIdentifier = { communicationUserId: '<USER_ID>' };
try {
activeCall = await callAgent.startCall([targetUserIdentifier]);
console.log('Call initiated successfully');
} catch (error) {
console.error('Error initiating call:', error);
}
}