修复:

1. 本地视频流能获取到了
2. 找到了视频连接错误的原因:挂断后本地麦克风和视频并没有关闭

现有问题:
1. 需要在挂断后关闭麦克风和摄像头
2. 没有渲染远程流推送的视频
This commit is contained in:
2025-07-15 09:17:47 +08:00
parent 32e676e320
commit 5ef233de6d
3 changed files with 1614 additions and 982 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -33,12 +33,36 @@ export const useChatStore = defineStore("chat", () => {
const isDisconnected = computed(() => callStatus.value === "disconnected")
const isHangup = computed(() => callStatus.value === "hangup")
// 时间戳日志函数
const logWithTime = (level, message, ...args) => {
const timestamp = new Date().toLocaleTimeString("zh-CN", {
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
fractionalSecondDigits: 3,
})
switch (level) {
case "log":
console.log(`[${timestamp}] ${message}`, ...args)
break
case "error":
console.error(`[${timestamp}] ${message}`, ...args)
break
case "warn":
console.warn(`[${timestamp}] ${message}`, ...args)
break
default:
console.log(`[${timestamp}] ${message}`, ...args)
}
}
// 监听通话状态变化
watch(
() => callStatus.value,
(newStatus) => {
console.log("通话状态变化:", newStatus)
logWithTime("log", "通话状态变化:", newStatus)
},
)
@@ -72,7 +96,7 @@ export const useChatStore = defineStore("chat", () => {
// 发送通话信令
const sendCallSignalInternal = async (signal) => {
try {
console.log("发送通话信令:", signal)
logWithTime("log", "发送通话信令:", signal)
if (!userStore.currentUser?.id) {
throw new Error("用户未登录")
@@ -99,13 +123,13 @@ export const useChatStore = defineStore("chat", () => {
call_status: signal.call_status,
}
console.log("发送通话信令请求数据:", requestData)
logWithTime("log", "发送通话信令请求数据:", requestData)
const response = await sendCallSignal(requestData)
console.log("发送通话信令成功:", signal)
logWithTime("log", "发送通话信令成功:", signal)
return response
} catch (error) {
console.error("发送通话信令失败:", error)
logWithTime("error", "发送通话信令失败:", error)
throw error
}
}
@@ -114,9 +138,9 @@ export const useChatStore = defineStore("chat", () => {
const initDB = async () => {
try {
await chatDB.init()
console.log("数据库初始化成功")
logWithTime("log", "数据库初始化成功")
} catch (error) {
console.error("数据库初始化失败:", error)
logWithTime("error", "数据库初始化失败:", error)
}
}
@@ -142,7 +166,7 @@ export const useChatStore = defineStore("chat", () => {
friends.value = friendList
} catch (error) {
console.error("加载好友列表失败:", error)
logWithTime("error", "加载好友列表失败:", error)
const friendList = presetUsers.filter((user) => user.id !== currentUserId)
friendList.forEach((friend) => {
friend.lastMessage = ""
@@ -171,7 +195,7 @@ export const useChatStore = defineStore("chat", () => {
const chatHistory = await chatDB.getChatHistory(currentUserId, friend.id)
messages.value = chatHistory.sort((a, b) => a.timestamp - b.timestamp)
} catch (error) {
console.error("加载聊天记录失败:", error)
logWithTime("error", "加载聊天记录失败:", error)
messages.value = []
}
}
@@ -197,7 +221,7 @@ export const useChatStore = defineStore("chat", () => {
try {
await chatDB.saveMessage(message, currentUserId, currentFriend.value.id)
} catch (error) {
console.error("保存消息失败:", error)
logWithTime("error", "保存消息失败:", error)
}
}
}
@@ -205,7 +229,7 @@ export const useChatStore = defineStore("chat", () => {
// 处理接收到的消息
const handleIncomingMessage = async (messageData, currentUserId) => {
if (messageData.isFromHttp) {
console.log("忽略来自HTTP的消息避免循环")
logWithTime("log", "忽略来自HTTP的消息避免循环")
return
}
@@ -254,7 +278,7 @@ export const useChatStore = defineStore("chat", () => {
await chatDB.updateFriendLastMessage(friend.id, currentUserId, lastMessage, friend.unreadCount)
}
} catch (error) {
console.error("处理接收消息失败:", error)
logWithTime("error", "处理接收消息失败:", error)
}
}
@@ -276,8 +300,8 @@ export const useChatStore = defineStore("chat", () => {
// 处理通话信令
const handleCallSignal = (data) => {
console.log("处理通话信令:", data)
console.log("通话状态:", data.call_status)
logWithTime("log", "处理通话信令:", data)
logWithTime("log", "通话状态:", data.call_status)
switch (data.call_status) {
case "invite":
@@ -325,81 +349,123 @@ export const useChatStore = defineStore("chat", () => {
}
}
// 处理Offer信令
// 修复问题2: 改进Offer处理允许重新协商
const handleOffer = async (data) => {
if (incomingCall.value && incomingCall.value.callId === data.call_id) {
if (callStatus.value === "ongoing") {
console.log("通话已建立,忽略重复的 Offer")
// 修复问题1&2: 不再严格检查ongoing状态允许重新协商
if (callStatus.value === "ongoing" && webrtc.isConnected.value) {
logWithTime("log", "通话已稳定连接,忽略重复的 Offer")
return
}
// 允许在connecting状态下重新处理offer重新协商
if (callStatus.value === "connecting" || callStatus.value === "failed") {
logWithTime("log", "连接中或失败状态,允许重新处理 Offer")
}
try {
const offer = JSON.parse(data.content || data.message_content || "{}")
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
console.log("📥 收到对方 OFFER 信令:")
console.log("📋 Offer 类型:", offer.type)
console.log("📋 Offer SDP (前200字符):", offer.sdp ? offer.sdp.substring(0, 200) + "..." : "无SDP")
console.log("📋 通话ID:", data.call_id)
console.log("📋 发送方:", data.sender_user_id)
console.log("📋 完整 Offer 对象:", offer)
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "📥 收到对方 OFFER 信令:")
logWithTime("log", "📋 Offer 类型:", offer.type)
logWithTime("log", "📋 Offer SDP (前200字符):", offer.sdp ? offer.sdp.substring(0, 200) + "..." : "无SDP")
logWithTime("log", "📋 通话ID:", data.call_id)
logWithTime("log", "📋 发送方:", data.sender_user_id)
logWithTime("log", "📋 当前通话状态:", callStatus.value)
logWithTime("log", "📋 WebRTC连接状态:", webrtc.peerConnection.value?.connectionState)
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
if (!webrtc.peerConnection.value) {
console.log("创建 WebRTC 连接以处理 Offer")
// 修复问题3: 如果连接失败或不存在,重新创建
if (
!webrtc.peerConnection.value ||
webrtc.peerConnection.value.connectionState === "failed" ||
webrtc.peerConnection.value.connectionState === "closed"
) {
logWithTime("log", "重新创建 WebRTC 连接以处理 Offer")
webrtc.createPeerConnection(data.call_id, data.sender_user_id)
}
const answer = await webrtc.createAnswer(offer, data.call_id, data.sender_user_id)
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
console.log("📤 准备发送 ANSWER 信令:")
console.log("📋 Answer 类型:", answer.type)
console.log("📋 Answer SDP (前200字符):", answer.sdp ? answer.sdp.substring(0, 200) + "..." : "无SDP")
console.log("📋 通话ID:", data.call_id)
console.log("📋 接收方:", data.sender_user_id)
console.log("📋 完整 Answer 对象:", answer)
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "📤 准备发送 ANSWER 信令:")
logWithTime("log", "📋 Answer 类型:", answer.type)
logWithTime("log", "📋 Answer SDP (前200字符):", answer.sdp ? answer.sdp.substring(0, 200) + "..." : "无SDP")
logWithTime("log", "📋 通话ID:", data.call_id)
logWithTime("log", "📋 接收方:", data.sender_user_id)
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
await callAPI.sendAnswer(userStore.currentUser.id, data.sender_user_id, data.call_id, answer, data.message_type)
console.log("✅ 发送Answer成功")
// 更新状态为连接中
if (callStatus.value !== "ongoing") {
callStatus.value = "connecting"
callConnectionStatus.value = "正在建立连接..."
}
logWithTime("log", "✅ 发送Answer成功")
} catch (error) {
console.error("处理Offer失败:", error)
rejectCall("failed")
logWithTime("error", "❌ 处理Offer失败:", error)
// 修复问题3: 处理失败时尝试重连而不是直接拒绝
if (webrtc.connectionAttempts.value < 3) {
logWithTime("log", "🔄 Offer处理失败尝试重连")
setTimeout(() => {
webrtc.attemptReconnection(data.call_id, data.sender_user_id)
}, 2000)
} else {
rejectCall("failed")
}
}
}
}
// 处理Answer信令
// 修复问题2: 改进Answer处理允许重新协商
const handleAnswer = async (data) => {
if (currentCall.value && currentCall.value.callId === data.call_id) {
if (callStatus.value === "ongoing") {
console.log("通话已建立,忽略重复的 Answer")
// 修复问题1&2: 不再严格检查ongoing状态允许重新协商
if (callStatus.value === "ongoing" && webrtc.isConnected.value) {
logWithTime("log", "通话已稳定连接,忽略重复的 Answer")
return
}
// 允许在connecting状态下重新处理answer重新协商
if (callStatus.value === "connecting" || callStatus.value === "failed") {
logWithTime("log", "连接中或失败状态,允许重新处理 Answer")
}
try {
const answer = JSON.parse(data.content || data.message_content || "{}")
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
console.log("📥 收到对方 ANSWER 信令:")
console.log("📋 Answer 类型:", answer.type)
console.log("📋 Answer SDP (前200字符):", answer.sdp ? answer.sdp.substring(0, 200) + "..." : "无SDP")
console.log("📋 通话ID:", data.call_id)
console.log("📋 发送方:", data.sender_user_id)
console.log("📋 完整 Answer 对象:", answer)
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "📥 收到对方 ANSWER 信令:")
logWithTime("log", "📋 Answer 类型:", answer.type)
logWithTime("log", "📋 Answer SDP (前200字符):", answer.sdp ? answer.sdp.substring(0, 200) + "..." : "无SDP")
logWithTime("log", "📋 通话ID:", data.call_id)
logWithTime("log", "📋 发送方:", data.sender_user_id)
logWithTime("log", "📋 当前通话状态:", callStatus.value)
logWithTime("log", "📋 WebRTC连接状态:", webrtc.peerConnection.value?.connectionState)
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
await webrtc.setRemoteAnswer(answer)
callStatus.value = "connecting"
callConnectionStatus.value = "正在建立连接..."
console.log("✅ Answer处理成功开始建立连接")
logWithTime("log", "✅ Answer处理成功开始建立连接")
} catch (error) {
console.error("处理Answer失败:", error)
endCall("failed")
logWithTime("error", "❌ 处理Answer失败:", error)
// 修复问题3: 处理失败时尝试重连而不是直接结束
if (webrtc.connectionAttempts.value < 3) {
logWithTime("log", "🔄 Answer处理失败尝试重连")
setTimeout(() => {
webrtc.attemptReconnection(data.call_id, currentCall.value.peerId)
}, 2000)
} else {
endCall("failed")
}
}
}
}
@@ -412,7 +478,7 @@ export const useChatStore = defineStore("chat", () => {
}
}
// 处理通话失败
// 修复问题3: 改进通话失败处理,增加重连逻辑
const handleCallFailed = (data) => {
if (
(currentCall.value && currentCall.value.callId === data.call_id) ||
@@ -423,35 +489,47 @@ export const useChatStore = defineStore("chat", () => {
const peerId = currentCall.value?.peerId || incomingCall.value?.callerId
const friend = friends.value.find((f) => f.id === peerId)
if (friend) {
const callMessage = {
type:
currentCall.value?.callType === "video" || incomingCall.value?.callType === "video"
? "video-call"
: "audio-call",
content: "[连接失败]",
time: new Date().toLocaleTimeString().slice(0, 5),
senderId: peerId,
read: true,
timestamp: Date.now(),
duration: 0,
// 修复问题3: 失败时尝试重连
if (webrtc.connectionAttempts.value < 3) {
logWithTime("log", "🔄 通话失败,尝试重连")
callConnectionStatus.value = `连接失败,正在重试 (${webrtc.connectionAttempts.value + 1}/3)`
setTimeout(() => {
webrtc.attemptReconnection(data.call_id, peerId)
}, 3000)
} else {
// 达到最大重试次数,记录失败消息
if (friend) {
const callMessage = {
type:
currentCall.value?.callType === "video" || incomingCall.value?.callType === "video"
? "video-call"
: "audio-call",
content: "[连接失败]",
time: new Date().toLocaleTimeString().slice(0, 5),
senderId: peerId,
read: true,
timestamp: Date.now(),
duration: 0,
}
chatDB.saveMessage(callMessage, userStore.currentUser.id, friend.id)
friend.lastMessage = "[连接失败]"
chatDB.updateFriendLastMessage(friend.id, userStore.currentUser.id, "[连接失败]", friend.unreadCount)
}
chatDB.saveMessage(callMessage, userStore.currentUser.id, friend.id)
friend.lastMessage = "[连接失败]"
chatDB.updateFriendLastMessage(friend.id, userStore.currentUser.id, "[连接失败]", friend.unreadCount)
setTimeout(() => {
resetCallState()
}, 5000)
}
setTimeout(() => {
resetCallState()
}, 5000)
}
}
// 处理来电
const handleIncomingCall = (data) => {
if (isInCall.value) {
console.log("当前正在通话中,发送忙线状态")
logWithTime("log", "当前正在通话中,发送忙线状态")
callAPI.sendBusy(userStore.currentUser.id, data.sender_user_id, data.call_id, data.message_type)
const friend = friends.value.find((f) => f.id === data.sender_user_id)
@@ -486,30 +564,30 @@ export const useChatStore = defineStore("chat", () => {
callError.value = null
callConnectionStatus.value = "收到来电"
console.log("收到来电:", incomingCall.value)
logWithTime("log", "收到来电:", incomingCall.value)
setCallTimeout(() => {
if (callStatus.value === "ringing" && incomingCall.value) {
console.log("来电超时,自动拒绝")
logWithTime("log", "来电超时,自动拒绝")
rejectCall("no-answer")
}
}, 30000)
}
// 处理通话接受
// 修复问题2&3: 改进通话接受处理,增加重连机制
const handleCallAccepted = async (data) => {
if (currentCall.value && currentCall.value.callId === data.call_id) {
if (callStatus.value === "connecting" || callStatus.value === "ongoing") {
console.log("通话已在连接中,忽略重复的 accepted 信令")
// 修复问题2: 允许在连接失败后重新接受
if (callStatus.value === "ongoing" && webrtc.isConnected.value) {
logWithTime("log", "通话已稳定连接,忽略重复的 accepted 信令")
return
}
callStatus.value = "connecting"
callError.value = null
callConnectionStatus.value = "对方已接听,正在连接..."
console.log("通话已被接受,开始交换信令")
logWithTime("log", "通话已被接受,开始交换信令")
// 显示友好提示
setTimeout(() => {
if (callStatus.value === "connecting") {
callConnectionStatus.value = "对方已接听,正在建立连接..."
@@ -518,23 +596,33 @@ export const useChatStore = defineStore("chat", () => {
try {
if (!webrtc.localStream.value) {
console.log("获取本地媒体用于创建Offer")
logWithTime("log", "获取本地媒体用于创建Offer")
await webrtc.getLocalMedia({
audio: true,
video: currentCall.value.callType === "video",
})
}
// 修复问题3: 如果连接失败,重新创建连接
if (
!webrtc.peerConnection.value ||
webrtc.peerConnection.value.connectionState === "failed" ||
webrtc.peerConnection.value.connectionState === "closed"
) {
logWithTime("log", "重新创建 WebRTC 连接")
webrtc.createPeerConnection(currentCall.value.callId, currentCall.value.peerId)
}
const offer = await webrtc.createOffer(currentCall.value.callId, currentCall.value.peerId)
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
console.log("📤 准备发送 OFFER 信令:")
console.log("📋 Offer 类型:", offer.type)
console.log("📋 Offer SDP (前200字符):", offer.sdp ? offer.sdp.substring(0, 200) + "..." : "无SDP")
console.log("📋 通话ID:", currentCall.value.callId)
console.log("📋 接收方:", currentCall.value.peerId)
console.log("📋 完整 Offer 对象:", offer)
console.log("🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
logWithTime("log", "📤 准备发送 OFFER 信令:")
logWithTime("log", "📋 Offer 类型:", offer.type)
logWithTime("log", "📋 Offer SDP (前200字符):", offer.sdp ? offer.sdp.substring(0, 200) + "..." : "无SDP")
logWithTime("log", "📋 通话ID:", currentCall.value.callId)
logWithTime("log", "📋 接收方:", currentCall.value.peerId)
logWithTime("log", "📋 连接尝试次数:", webrtc.connectionAttempts.value)
logWithTime("log", "🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯🎯")
await callAPI.sendOffer(
userStore.currentUser.id,
@@ -544,10 +632,23 @@ export const useChatStore = defineStore("chat", () => {
currentCall.value.callType === "video" ? 6 : 7,
)
console.log("✅ 发送Offer成功")
logWithTime("log", "✅ 发送Offer成功")
} catch (error) {
console.error("创建Offer失败:", error)
endCall("failed")
logWithTime("error", "❌ 创建Offer失败:", error)
// 修复问题3: 创建失败时尝试重连
if (webrtc.connectionAttempts.value < 3) {
logWithTime("log", "🔄 创建Offer失败尝试重连")
setTimeout(() => {
webrtc.attemptReconnection(currentCall.value.callId, currentCall.value.peerId)
// 重连后重新尝试处理accepted信令
setTimeout(() => {
handleCallAccepted(data)
}, 1000)
}, 2000)
} else {
endCall("failed")
}
}
}
}
@@ -557,7 +658,7 @@ export const useChatStore = defineStore("chat", () => {
if (currentCall.value && currentCall.value.callId === data.call_id) {
resetCallState()
callConnectionStatus.value = "对方已拒绝"
console.log("通话已被拒绝")
logWithTime("log", "通话已被拒绝")
const friend = friends.value.find((f) => f.id === currentCall.value.peerId)
if (friend) {
@@ -619,7 +720,7 @@ export const useChatStore = defineStore("chat", () => {
resetCallState()
callConnectionStatus.value = "通话已结束"
console.log("通话已结束")
logWithTime("log", "通话已结束")
setTimeout(() => {
callConnectionStatus.value = ""
@@ -635,11 +736,11 @@ export const useChatStore = defineStore("chat", () => {
) {
try {
const candidate = JSON.parse(data.content || data.message_content || "{}")
console.log("收到ICE候选:", candidate)
logWithTime("log", "收到ICE候选:", candidate)
webrtc.addIceCandidate(candidate)
} catch (error) {
console.error("处理ICE候选失败:", error)
logWithTime("error", "处理ICE候选失败:", error)
}
}
}
@@ -666,7 +767,7 @@ export const useChatStore = defineStore("chat", () => {
resetCallState()
callConnectionStatus.value = "对方无人接听"
console.log("对方无人接听")
logWithTime("log", "对方无人接听")
setTimeout(() => {
callConnectionStatus.value = ""
@@ -696,7 +797,7 @@ export const useChatStore = defineStore("chat", () => {
resetCallState()
callConnectionStatus.value = "对方忙线中"
console.log("对方忙线中")
logWithTime("log", "对方忙线中")
setTimeout(() => {
callConnectionStatus.value = ""
@@ -734,7 +835,7 @@ export const useChatStore = defineStore("chat", () => {
callStatus.value = "hangup"
callConnectionStatus.value = "对方已挂断"
console.log("对方已挂断通话")
logWithTime("log", "对方已挂断通话")
setTimeout(() => {
resetCallState()
@@ -772,7 +873,7 @@ export const useChatStore = defineStore("chat", () => {
callStatus.value = "disconnected"
callConnectionStatus.value = "网络连接已断开"
console.log("网络连接已断开")
logWithTime("log", "网络连接已断开")
setTimeout(() => {
resetCallState()
@@ -808,7 +909,7 @@ export const useChatStore = defineStore("chat", () => {
resetCallState()
callConnectionStatus.value = "对方已终止呼叫"
console.log("对方已终止呼叫")
logWithTime("log", "对方已终止呼叫")
setTimeout(() => {
callConnectionStatus.value = ""
@@ -851,14 +952,14 @@ export const useChatStore = defineStore("chat", () => {
// 立即获取本地媒体并显示
try {
console.log("发起通话,立即获取本地媒体...")
logWithTime("log", "发起通话,立即获取本地媒体...")
await webrtc.getLocalMedia({
audio: true,
video: type === "video",
})
console.log("本地媒体获取成功,可以显示自己的摄像头")
logWithTime("log", "本地媒体获取成功,可以显示自己的摄像头")
} catch (error) {
console.warn("获取本地媒体失败,但继续通话流程:", error)
logWithTime("warn", "获取本地媒体失败,但继续通话流程:", error)
}
// 设置30秒超时
@@ -866,7 +967,7 @@ export const useChatStore = defineStore("chat", () => {
if (callStatus.value === "calling") {
resetCallState()
callConnectionStatus.value = "对方无人接听"
console.log("呼叫超时,对方无人接听")
logWithTime("log", "呼叫超时,对方无人接听")
callAPI.sendNoAnswer(userStore.currentUser.id, peerId, callId, type === "video" ? 6 : 7)
@@ -892,7 +993,7 @@ export const useChatStore = defineStore("chat", () => {
// 发送通话请求
try {
console.log("准备发送通话邀请:", {
logWithTime("log", "准备发送通话邀请:", {
senderUserId: userStore.currentUser.id,
receiverUserId: peerId,
callId: callId,
@@ -901,9 +1002,9 @@ export const useChatStore = defineStore("chat", () => {
await callAPI.invite(userStore.currentUser.id, peerId, callId, type === "video" ? 6 : 7)
console.log("发起通话成功:", currentCall.value)
logWithTime("log", "发起通话成功:", currentCall.value)
} catch (error) {
console.error("发起通话失败:", error)
logWithTime("error", "发起通话失败:", error)
resetCallState()
let errorMessage = "发起通话失败"
@@ -921,20 +1022,19 @@ export const useChatStore = defineStore("chat", () => {
// 接听来电
const acceptCall = async () => {
// if (isConnecting) return;
if (!incomingCall.value) {
console.log("没有来电,无法接听")
logWithTime("log", "没有来电,无法接听")
return
}
if (callStatus.value === "connecting" || callStatus.value === "ongoing") {
console.log("通话已在进行中,忽略重复接听")
logWithTime("log", "通话已在进行中,忽略重复接听")
return
}
clearCallTimeout()
console.log("开始接听通话:", incomingCall.value)
logWithTime("log", "开始接听通话:", incomingCall.value)
callStatus.value = "connecting"
callConnectionStatus.value = "正在连接..."
@@ -946,9 +1046,9 @@ export const useChatStore = defineStore("chat", () => {
audio: true,
video: incomingCall.value.callType === "video",
})
console.log("接听时获取本地媒体成功")
logWithTime("log", "接听时获取本地媒体成功")
} catch (mediaError) {
console.warn("接听时获取本地媒体失败,但继续通话流程:", mediaError)
logWithTime("warn", "接听时获取本地媒体失败,但继续通话流程:", mediaError)
}
// 发送接受信号
@@ -967,9 +1067,9 @@ export const useChatStore = defineStore("chat", () => {
status: "connecting",
}
console.log("接听通话成功,等待信令交换...")
logWithTime("log", "接听通话成功,等待信令交换...")
} catch (error) {
console.error("接听通话失败:", error)
logWithTime("error", "接听通话失败:", error)
rejectCall("failed")
}
}
@@ -977,16 +1077,16 @@ export const useChatStore = defineStore("chat", () => {
// 拒绝来电
const rejectCall = async (reason = "rejected") => {
if (!incomingCall.value) {
console.log("没有来电,无法拒绝")
logWithTime("log", "没有来电,无法拒绝")
return
}
if (callStatus.value === "connecting" || callStatus.value === "ongoing") {
console.log("通话已在进行中,无法拒绝")
logWithTime("log", "通话已在进行中,无法拒绝")
return
}
console.log("开始拒绝通话:", reason)
logWithTime("log", "开始拒绝通话:", reason)
try {
if (reason === "rejected") {
@@ -1031,9 +1131,9 @@ export const useChatStore = defineStore("chat", () => {
callConnectionStatus.value = ""
}, 5000)
console.log("拒绝通话成功")
logWithTime("log", "拒绝通话成功")
} catch (error) {
console.error("拒绝通话失败:", error)
logWithTime("error", "拒绝通话失败:", error)
}
}
@@ -1089,9 +1189,9 @@ export const useChatStore = defineStore("chat", () => {
callConnectionStatus.value = ""
}, 5000)
console.log("结束通话成功")
logWithTime("log", "结束通话成功")
} catch (error) {
console.error("结束通话失败:", error)
logWithTime("error", "结束通话失败:", error)
resetCallState()
}
}