WXL
2025-08-06 2eff945fb9fc0d17b2098b26aba74ab192ec3727
src/utils/sipService.js
@@ -5,6 +5,7 @@
    this.ua = null;
    this.currentSession = null;
    this.onStatusChange = null; // 状态变化回调
    this.onCallStatusChange = null; // 新增通话状态回调
  }
  // 初始化SIP客户端
@@ -97,10 +98,18 @@
        "Allow: INVITE, ACK, BYE, CANCEL, OPTIONS",
      ],
      eventHandlers: {
        progress: (e) => console.log("呼叫中..."),
        failed: (e) => console.error("呼叫失败:", e),
        ended: (e) => console.log("通话结束"),
        confirmed: (e) => console.log("通话已接通"),
        progress: (e) => {
          this.updateCallStatus("calling", "呼叫中...");
        },
        failed: (e) => {
          this.updateCallStatus("ended", `呼叫失败: ${e.cause}`);
        },
        ended: (e) => {
          this.updateCallStatus("ended", "通话结束");
        },
        confirmed: (e) => {
          this.updateCallStatus("connected", "通话已接通");
        },
      },
      mediaConstraints: {
        audio: true,
@@ -135,6 +144,7 @@
    );
    // 在会话创建后修改 SDP
    this.currentSession.on("peerconnection", (pc) => {
       this.updateCallStatus('calling', '呼叫中...');
      pc.createOffer = (offerOptions) => {
        return RTCPeerConnection.prototype.createOffer
          .call(pc, offerOptions)
@@ -148,6 +158,17 @@
            });
          });
      };
    });
    this.currentSession.on('failed', (e) => {
        this.updateCallStatus('failed', `呼叫失败2: ${e.cause}`);
      });
      this.currentSession.on('ended', () => {
        this.updateCallStatus('ended', '通话已结束');
      });
      this.currentSession.on('confirmed', () => {
        this.updateCallStatus('connected', '通话已接通');
    });
    this.setupAudio(this.currentSession);
  }
@@ -163,10 +184,17 @@
  endCall() {
    if (this.currentSession) {
      this.currentSession.terminate();
      this.updateCallStatus('ended', '通话已结束');
      this.currentSession = null;
    }
  }
  // 新增方法:更新通话状态
  updateCallStatus(type, text) {
    console.log(`通话状态更新: ${type} - ${text}`);
    if (this.onCallStatusChange) {
      this.onCallStatusChange({ type, text });
    }
  }
}
export default new SipService();