From dafbb909e478015ee062bf962bddcb20a6fed55c Mon Sep 17 00:00:00 2001 From: WXL <1785969728@qq.com> Date: 星期四, 03 七月 2025 14:51:32 +0800 Subject: [PATCH] 测试完成 --- src/utils/sipService.js | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 87 insertions(+), 0 deletions(-) diff --git a/src/utils/sipService.js b/src/utils/sipService.js new file mode 100644 index 0000000..95ac836 --- /dev/null +++ b/src/utils/sipService.js @@ -0,0 +1,87 @@ +import JsSIP from 'jssip' + +class SipService { + constructor() { + this.ua = null + this.currentSession = 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 + }) + + this.ua.start() + + // 娉ㄥ唽浜嬩欢鐩戝惉 + this.ua.on('registered', () => { + console.log('SIP娉ㄥ唽鎴愬姛') + }) + + this.ua.on('registrationFailed', (e) => { + console.error('SIP娉ㄥ唽澶辫触:', e) + }) + + // 鐩戝惉鏉ョ數 + this.ua.on('newRTCSession', (data) => { + this.handleIncomingCall(data.session) + }) + } + + // 涓�閿嫧鍙� + makeCall(targetNumber) { + if (!this.ua) { + console.error('SIP瀹㈡埛绔湭鍒濆鍖�') + return + } + + const options = { + eventHandlers: { + progress: (e) => console.log('鍛煎彨涓�...'), + failed: (e) => console.error('鍛煎彨澶辫触:', e), + ended: (e) => console.log('閫氳瘽缁撴潫'), + confirmed: (e) => console.log('閫氳瘽宸叉帴閫�') + }, + mediaConstraints: { audio: true, video: false }, + rtcOfferConstraints: { offerToReceiveAudio: 1 } + } + + this.currentSession = this.ua.call(`sip:${targetNumber}`, options) + this.setupAudio(this.currentSession) + } + + // 鎸傛柇褰撳墠閫氳瘽 + endCall() { + if (this.currentSession) { + this.currentSession.terminate() + this.currentSession = null + } + } + + // 澶勭悊闊抽娴� + setupAudio(session) { + session.connection.addEventListener('addstream', (e) => { + const audioElement = document.getElementById('remoteAudio') + if (audioElement) { + audioElement.srcObject = e.stream + } + }) + } + + // 澶勭悊鏉ョ數 + handleIncomingCall(session) { + if (session.direction === 'incoming') { + console.log('鏉ョ數:', session.remote_identity.uri.toString()) + // 杩欓噷鍙互瑙﹀彂UI閫氱煡 + } + } +} + +export default new SipService() -- Gitblit v1.9.3