From 66cc734e81fe0497d88e0951b6aee9c9c8342f2c Mon Sep 17 00:00:00 2001 From: WXL (wul) <wl_5969728@163.com> Date: 星期三, 17 九月 2025 11:43:07 +0800 Subject: [PATCH] 电话更新 --- src/utils/sipService.js | 53 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/utils/sipService.js b/src/utils/sipService.js index df2cda9..dbc4a4e 100644 --- a/src/utils/sipService.js +++ b/src/utils/sipService.js @@ -7,6 +7,8 @@ this.onStatusChange = null; this.onCallStatusChange = null; this.onIncomingCall = null; + this.isRegistered = false; // 鏂板娉ㄥ唽鐘舵�佹爣蹇� + this.registrationTime = null; // 鏂板娉ㄥ唽鎴愬姛鏃堕棿鎴� } init(config) { @@ -28,12 +30,25 @@ this.ua.start(); // 浜嬩欢鐩戝惉 - this.ua.on("registered", () => - this.updateStatus("registered", "宸叉敞鍐�56") - ); - this.ua.on("registrationFailed", (e) => - this.updateStatus("failed", `娉ㄥ唽澶辫触11: ${e.cause}`) - ); + this.ua.on("registered", () => { + this.isRegistered = true; + this.registrationTime = Date.now(); // 璁板綍娉ㄥ唽鎴愬姛鏃堕棿 + console.log(this.registrationTime, "娉ㄥ唽鏃堕棿"); + + this.updateStatus("registered", "宸叉敞鍐�"); + }); + + this.ua.on("registrationFailed", (e) => { + this.isRegistered = false; + this.updateStatus("failed", `娉ㄥ唽澶辫触: ${e.cause}`); + }); + + this.ua.on("unregistered", () => { + this.isRegistered = false; + let registrationTime = Date.now(); // 璁板綍娉ㄩ攢鎴愬姛鏃堕棿 + console.log(registrationTime, "娉ㄩ攢鏃堕棿"); + this.updateStatus("disconnected", "宸叉敞閿�"); + }); this.ua.on("disconnected", () => this.updateStatus("disconnected", "杩炴帴鏂紑") ); @@ -49,8 +64,30 @@ throw error; } } + // 鏂板鏂规硶锛氭鏌ユ槸鍚﹀彲浠ュ懠鍙� + canMakeCall(minDelay = 2000) { + if (!this.isRegistered) { + return { canCall: false, reason: "SIP鏈敞鍐岋紝鏃犳硶鍛煎彨" }; + } + const now = Date.now(); + const timeSinceRegistration = now - this.registrationTime; + + if (timeSinceRegistration < minDelay) { + const remaining = minDelay - timeSinceRegistration; + return { + canCall: false, + reason: `娉ㄥ唽鎴愬姛锛岃绛夊緟 ${Math.ceil(remaining / 1000)} 绉掑悗鍐嶅懠鍙玚, + }; + } + + return { canCall: true, reason: "" }; + } makeCall(targetNumber) { + const { canCall, reason } = this.canMakeCall(); + if (!canCall) { + return Promise.reject(new Error(reason)); + } return new Promise((resolve, reject) => { try { if (!this.ua) { @@ -84,7 +121,7 @@ }; this.currentSession = this.ua.call( - `sip:${targetNumber}@192.169.129.198`, + `sip:${targetNumber}@1192.170.66.107`, options ); @@ -165,7 +202,7 @@ errorMessage = "浼氳瘽鍙傛暟涓嶆弧瓒虫湇鍔″櫒瑕佹眰"; break; default: - errorMessage = `鍛煎彨澶辫触: ${e.cause || e.message}`; + errorMessage = `鍛煎彨澶辫触3: ${e.cause || e.message}`; } this.updateCallStatus("failed55", errorMessage); -- Gitblit v1.9.3