From 3ae495d3c3e95019b9e0066aae3c3b35802c51fe Mon Sep 17 00:00:00 2001 From: WXL <1785969728@qq.com> Date: 星期一, 07 七月 2025 16:24:12 +0800 Subject: [PATCH] 测试完成 --- src/utils/sipService.js | 78 +++++++++++++++++++++++++++------------ 1 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/utils/sipService.js b/src/utils/sipService.js index 95ac836..9c095dd 100644 --- a/src/utils/sipService.js +++ b/src/utils/sipService.js @@ -4,42 +4,72 @@ constructor() { this.ua = null this.currentSession = null + this.onStatusChange = null // 鐘舵�佸彉鍖栧洖璋� } // 鍒濆鍖朣IP瀹㈡埛绔� init(config) { - this.ua = new JsSIP.UA({ - sockets: [new JsSIP.WebSocketInterface(config.wsUrl)], - uri: config.sipUri, - password: config.password, - display_name: config.displayName, - realm: config.realm, - ha1: config.ha1, - register: true - }) + try { + this.updateStatus('connecting', '杩炴帴涓�...') - this.ua.start() + this.ua = new JsSIP.UA({ + sockets: [new JsSIP.WebSocketInterface(config.wsUrl)], + uri: config.sipUri, + password: config.password, + display_name: config.displayName, + realm: config.realm, + register: true, + register_expires: 300, // 娉ㄥ唽鏈夋晥鏈�(绉�) + connection_recovery_min_interval: 2, // 鏈�灏忛噸杩為棿闅� + connection_recovery_max_interval: 30 // 鏈�澶ч噸杩為棿闅� + }) - // 娉ㄥ唽浜嬩欢鐩戝惉 - this.ua.on('registered', () => { - console.log('SIP娉ㄥ唽鎴愬姛') - }) + this.ua.start() - this.ua.on('registrationFailed', (e) => { - console.error('SIP娉ㄥ唽澶辫触:', e) - }) + // 娉ㄥ唽浜嬩欢鐩戝惉 + this.ua.on('registered', () => { + this.updateStatus('registered', '宸叉敞鍐�') + }) - // 鐩戝惉鏉ョ數 - this.ua.on('newRTCSession', (data) => { - this.handleIncomingCall(data.session) - }) + this.ua.on('registrationFailed', (e) => { + this.updateStatus('failed', `娉ㄥ唽澶辫触: ${e.cause}`) + }) + + this.ua.on('disconnected', () => { + this.updateStatus('disconnected', '杩炴帴鏂紑') + }) + + this.ua.on('connected', () => { + this.updateStatus('connecting', '閲嶆柊杩炴帴涓�...') + }) + + // 鐩戝惉鏉ョ數 + this.ua.on('newRTCSession', (data) => { + this.handleIncomingCall(data.session) + }) + + } catch (error) { + this.updateStatus('failed', `鍒濆鍖栧け璐�: ${error.message}`) + console.error('SIP鍒濆鍖栧け璐�:', error) + } } - // 涓�閿嫧鍙� + // 鏇存柊鐘舵�佸苟閫氱煡UI + updateStatus(type, text) { + console.log(`SIP鐘舵�佹洿鏂�: ${type} - ${text}`) + if (this.onStatusChange) { + this.onStatusChange({ type, text }) + } + } + + // 涓�閿嫧鍙� - 澧炲姞娉ㄥ唽鐘舵�佹鏌� makeCall(targetNumber) { if (!this.ua) { - console.error('SIP瀹㈡埛绔湭鍒濆鍖�') - return + throw new Error('SIP瀹㈡埛绔湭鍒濆鍖�') + } + + if (!this.ua.isRegistered()) { + throw new Error('SIP鏈敞鍐岋紝鏃犳硶鍛煎彨') } const options = { -- Gitblit v1.9.3