SDKsuni-app / uni-app x商业版
处理通话事件
集中监听邀请、接听、参与者和挂断等通话状态变化。
通话状态层应集中注册邀请生命周期和参与者事件。这些事件的回调参数统一声明为 string,没有定义字符串内部字段。应用可把事件作为状态刷新信号,再通过查询接口取得类型明确的邀请或房间信息。
| 事件 | 建议处理 |
|---|---|
onReceiveNewInvitation | 查询并恢复当前待处理邀请,显示来电界面。 |
onInviteeAccepted | 刷新当前通话状态,继续媒体连接流程。 |
onInviteeAcceptedByOtherDevice | 当前设备结束来电等待,避免重复接听。 |
onInviteeRejected | 刷新被邀请人的状态;无人可接听时结束邀请。 |
onInviteeRejectedByOtherDevice | 当前设备关闭来电界面。 |
onInvitationCancelled、onInvitationTimeout | 结束等待状态并释放预创建资源。 |
onHangUp | 离开媒体房间并结束通话。 |
onRoomParticipantConnected、onRoomParticipantDisconnected | 重新获取当前房间或由媒体引擎刷新参与者列表。 |
onStreamChange | Android、iOS 可作为媒体状态变化提示;HarmonyOS 不支持此事件。 |
下面把事件当作刷新信号,不假设载荷结构。来电事件通过 signalingGetInvitationInfoStartApp() 取得类型明确的邀请;群通话可以通过 signalingGetRoomByGroupID() 重新查询当前房间。
import {
off,
onHangUp,
onInvitationCancelled,
onInvitationTimeout,
onInviteeAccepted,
onInviteeAcceptedByOtherDevice,
onInviteeRejected,
onInviteeRejectedByOtherDevice,
onReceiveNewInvitation,
onRoomParticipantConnected,
onRoomParticipantDisconnected,
onStreamChange,
signalingGetInvitationInfoStartApp,
} from '@/uni_modules/unix-openim-sdk'
const restoreIncomingInvitation = async () => {
const result = await signalingGetInvitationInfoStartApp({
userID: currentUserID,
})
if (result?.invitation?.roomID != null) {
showIncomingCall(result.invitation!)
}
}
const handleIncomingInvitation = (_payload: string) => {
void restoreIncomingInvitation()
}
const refreshCall = (_payload: string) => {
void refreshCurrentCall()
}
const finishCurrentCall = (_payload: string) => {
closeCallAndReleaseMedia()
}
const incomingSubscription = onReceiveNewInvitation(handleIncomingInvitation)
const acceptedSubscription = onInviteeAccepted(refreshCall)
const acceptedElsewhereSubscription = onInviteeAcceptedByOtherDevice(finishCurrentCall)
const rejectedSubscription = onInviteeRejected(refreshCall)
const rejectedElsewhereSubscription = onInviteeRejectedByOtherDevice(finishCurrentCall)
const cancelledSubscription = onInvitationCancelled(finishCurrentCall)
const timeoutSubscription = onInvitationTimeout(finishCurrentCall)
const hangUpSubscription = onHangUp(finishCurrentCall)
const participantConnectedSubscription = onRoomParticipantConnected(refreshCall)
const participantDisconnectedSubscription = onRoomParticipantDisconnected(refreshCall)
const streamChangeSubscription = onStreamChange(refreshCall)
function removeCallListeners() {
off(incomingSubscription)
off(acceptedSubscription)
off(acceptedElsewhereSubscription)
off(rejectedSubscription)
off(rejectedElsewhereSubscription)
off(cancelledSubscription)
off(timeoutSubscription)
off(hangUpSubscription)
off(participantConnectedSubscription)
off(participantDisconnectedSubscription)
off(streamChangeSubscription)
}如果应用已约定事件字符串协议,可以在单独的适配层解析;业务状态层应校验字段、协议版本和 roomID,解析失败时回退到查询。
退出登录、切换账号或销毁全局通话状态层时调用 removeCallListeners()。自定义信令事件单独归属发送自定义信令。
这个页面有帮助吗?