| | |
| | | constructor() { |
| | | this.ua = null |
| | | this.currentSession = null |
| | | this.onStatusChange = null // 状态变化回调 |
| | | } |
| | | |
| | | // 初始化SIP客户端 |
| | | 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 = { |