SIP.js v0.20.0订阅Presence未收到Notify回调问题求助
需求:订阅所有SIP用户的presence与dialog状态,在用户通话、在线、振铃、注销时获取Notify通知。
技术栈:React + SIP.js v0.20.0
现状:已配置UserAgent并完成注册,通过Subscriber订阅用户10019的presence事件,服务器已接受订阅请求,但未收到10019注册/注销等状态变更的Notify回调。
相关代码
UserAgent配置
const userOptions: UserAgentOptions = { uri: UAURI, authorizationPassword: password, authorizationUsername: username, contactName: username, viaHost: domain, transportOptions: { server: server, traceSip: true, }, delegate: { onInvite, onNotify }, register: true, noAnswerTimeout: 60, userAgentString: "A | B ", dtmfType: "info", displayName: "WEBRTC", activeAfterTransfer: false, // Die when the transfer is completed logBuiltinEnabled: true, // Boolean - true or false - If true throws console logs }; console.log( userOptions ); userAgent = new UserAgent( userOptions ); console.log( "USSSSSEEERRR AGENT" ); console.log( userAgent );
订阅逻辑
const Subscribe = () => { console.log("useragentstate"); console.log(userAgent); const dialogOptions = { extraHeaders: [ "Accept: application/pidf+xml" ], }; const targetURI = new URI("sip", "10019", "example.com"); const eventType = "presence"; const subscriber = new Subscriber(userAgent, targetURI, eventType, dialogOptions); subscriber.delegate = { onNotify: (notification: Notification) => { // here you can get the NOTIFY body and parse it to get the users inside the conference... console.log("COMING FOR NOTIFY"); notification.accept(); } }; console.log(subscriber); subscriber.subscribe(); };
处理订阅有效期与自动刷新:SIP订阅默认有效期较短,服务器不会永久推送状态。需在订阅时指定有效期,并添加刷新逻辑:
修改dialogOptions添加有效期头:const dialogOptions = { extraHeaders: [ "Accept: application/pidf+xml", "Expires: 3600" // 设置1小时有效期,按需调整 ], };同时给Subscriber添加刷新回调:
subscriber.delegate = { onNotify: (notification: Notification) => { console.log("COMING FOR NOTIFY"); notification.accept(); }, onRefresh: () => { subscriber.subscribe(); // 订阅过期前自动刷新 } };避免Notify回调冲突:你在UserAgent的delegate中也配置了
onNotify,可能会和Subscriber的回调产生拦截或覆盖。建议移除UserAgent配置中的onNotify,让Subscriber单独处理presence相关通知:const userOptions: UserAgentOptions = { // ... 其他配置 delegate: { onInvite }, // 移除onNotify // ... 其他配置 };验证服务器配置:确认SIP服务器已开启presence模块,且当前订阅用户拥有查看目标用户10019状态的权限。部分服务器默认禁止跨用户的presence订阅,需手动配置权限。
核对SIP消息日志:利用
traceSip: true查看浏览器控制台中的SIP消息,确认服务器在用户状态变更时是否发送了Notify请求。若服务器未发送,问题出在服务器端;若发送了但客户端未触发回调,再聚焦客户端代码排查。确认目标URI正确性:确保
new URI("sip", "10019", "example.com")中的域名与服务器实际域名一致,避免因目标用户不存在导致服务器无法推送状态。
内容的提问来源于stack exchange,提问作者Pratik Patel

