| | |
| | | this.onStatusChange = null; |
| | | this.onCallStatusChange = null; |
| | | this.onIncomingCall = null; |
| | | this.isRegistered = false; // 新增注册状态标志 |
| | | this.registrationTime = null; // 新增注册成功时间戳 |
| | | } |
| | | |
| | | init(config) { |
| | |
| | | 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(); // 记录注册成功时间 |
| | | this.updateStatus("registered", "已注册"); |
| | | }); |
| | | |
| | | this.ua.on("registrationFailed", (e) => { |
| | | this.isRegistered = false; |
| | | this.updateStatus("failed", `注册失败: ${e.cause}`); |
| | | }); |
| | | |
| | | this.ua.on("unregistered", () => { |
| | | this.isRegistered = false; |
| | | this.updateStatus("disconnected", "已注销"); |
| | | }); |
| | | this.ua.on("disconnected", () => |
| | | this.updateStatus("disconnected", "连接断开") |
| | | ); |
| | |
| | | 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) { |