目前对方可以接收到通话申请了(接电话有通话中判断错误导致重复send-to-user路由的问题)
This commit is contained in:
@@ -16,6 +16,8 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const currentCall = ref(null)
|
||||
const incomingCall = ref(null)
|
||||
const callStatus = ref("idle") // idle, calling, ringing, ongoing, ended
|
||||
const callError = ref(null) // 错误状态:'rejected', 'no-answer', 'busy'
|
||||
const callConnectionStatus = ref("") // 连接状态:等待接听、正在连接等
|
||||
|
||||
const webrtc = useWebRTC()
|
||||
const userStore = useUserStore()
|
||||
@@ -69,7 +71,7 @@ export const useChatStore = defineStore("chat", () => {
|
||||
friend.lastMessage = ""
|
||||
friend.unreadCount = 0
|
||||
})
|
||||
friends.value = friend极
|
||||
friends.value = friendList
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,6 +132,12 @@ export const useChatStore = defineStore("chat", () => {
|
||||
|
||||
// 处理接收到的消息
|
||||
const handleIncomingMessage = async (messageData, currentUserId) => {
|
||||
// 添加来源检查 - 防止循环
|
||||
if (messageData.isFromHttp) {
|
||||
console.log("忽略来自HTTP的消息,避免循环")
|
||||
return
|
||||
}
|
||||
|
||||
const senderId = messageData.sender_user_id
|
||||
const receiverId = messageData.receiver_user_id
|
||||
|
||||
@@ -162,7 +170,6 @@ export const useChatStore = defineStore("chat", () => {
|
||||
} else {
|
||||
// 更新未读消息计数
|
||||
const friend = friends.value.find((f) => f.id == senderId)
|
||||
console.log(f.id == senderId, senderId, f)
|
||||
if (friend) {
|
||||
friend.unreadCount = (friend.unreadCount || 0) + 1
|
||||
|
||||
@@ -213,11 +220,40 @@ export const useChatStore = defineStore("chat", () => {
|
||||
case "candidate":
|
||||
handleIceCandidate(data)
|
||||
break
|
||||
case "no-answer": // 添加无人接听状态
|
||||
handleCallNoAnswer(data)
|
||||
break
|
||||
case "connecting": // 新增连接状态
|
||||
callConnectionStatus.value = "正在连接..."
|
||||
break
|
||||
case "busy": // 对方忙线中
|
||||
handleCallBusy(data)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// 处理来电
|
||||
const handleIncomingCall = (data) => {
|
||||
// 如果自己正在通话中,自动拒绝并发送忙线状态
|
||||
if (isInCall.value) {
|
||||
sendMessage({
|
||||
senderId: userStore.currentUser?.id,
|
||||
receiverId: data.sender_user_id,
|
||||
type: data.message_type === 6 ? "video-call" : "audio-call",
|
||||
callId: data.call_id,
|
||||
callStatus: "busy"
|
||||
})
|
||||
|
||||
// 添加未接来电消息
|
||||
const friend = friends.value.find(f => f.id === data.sender_user_id)
|
||||
if (friend) {
|
||||
friend.lastMessage = "[未接来电]"
|
||||
chatDB.updateFriendLastMessage(friend.id, userStore.currentUser.id, "[未接来电]", friend.unreadCount)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
incomingCall.value = {
|
||||
callId: data.call_id,
|
||||
callerId: data.sender_user_id,
|
||||
@@ -225,6 +261,8 @@ export const useChatStore = defineStore("chat", () => {
|
||||
content: data.content
|
||||
}
|
||||
callStatus.value = "ringing"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = "收到来电"
|
||||
|
||||
console.log("收到来电:", incomingCall.value)
|
||||
}
|
||||
@@ -233,6 +271,8 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const handleCallAccepted = (data) => {
|
||||
if (currentCall.value && currentCall.value.callId === data.call_id) {
|
||||
callStatus.value = "ongoing"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = "通话中"
|
||||
console.log("通话已被接受")
|
||||
}
|
||||
}
|
||||
@@ -241,8 +281,17 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const handleCallRejected = (data) => {
|
||||
if (currentCall.value && currentCall.value.callId === data.call_id) {
|
||||
callStatus.value = "ended"
|
||||
callError.value = "rejected"
|
||||
callConnectionStatus.value = "对方已拒绝"
|
||||
currentCall.value = null
|
||||
console.log("通话已被拒绝")
|
||||
|
||||
// 5秒后清除状态
|
||||
setTimeout(() => {
|
||||
callStatus.value = "idle"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = ""
|
||||
}, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,9 +300,17 @@ export const useChatStore = defineStore("chat", () => {
|
||||
if ((currentCall.value && currentCall.value.callId === data.call_id) ||
|
||||
(incomingCall.value && incomingCall.value.callId === data.call_id)) {
|
||||
callStatus.value = "ended"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = "通话已结束"
|
||||
currentCall.value = null
|
||||
incomingCall.value = null
|
||||
console.log("通话已结束")
|
||||
|
||||
// 5秒后清除状态
|
||||
setTimeout(() => {
|
||||
callStatus.value = "idle"
|
||||
callConnectionStatus.value = ""
|
||||
}, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -265,21 +322,102 @@ export const useChatStore = defineStore("chat", () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 处理无人接听
|
||||
const handleCallNoAnswer = (data) => {
|
||||
if (currentCall.value && currentCall.value.callId === data.call_id) {
|
||||
callStatus.value = "ended"
|
||||
callError.value = "no-answer"
|
||||
callConnectionStatus.value = "对方无人接听"
|
||||
currentCall.value = null
|
||||
console.log("对方无人接听")
|
||||
|
||||
// 5秒后清除状态
|
||||
setTimeout(() => {
|
||||
callStatus.value = "idle"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = ""
|
||||
}, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理对方忙线
|
||||
const handleCallBusy = (data) => {
|
||||
if (currentCall.value && currentCall.value.callId === data.call_id) {
|
||||
callStatus.value = "ended"
|
||||
callError.value = "busy"
|
||||
callConnectionStatus.value = "对方忙线中"
|
||||
currentCall.value = null
|
||||
console.log("对方忙线中")
|
||||
|
||||
// 5秒后清除状态
|
||||
setTimeout(() => {
|
||||
callStatus.value = "idle"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = ""
|
||||
}, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
// 发起通话
|
||||
const startCall = (peerId, type) => {
|
||||
if (isInCall.value) return
|
||||
if (isInCall.value) {
|
||||
alert('您正在通话中,请先结束当前通话。')
|
||||
return
|
||||
}
|
||||
|
||||
const friend = friends.value.find(f => f.id === peerId)
|
||||
if (!friend) {
|
||||
alert('好友信息无效')
|
||||
return
|
||||
}
|
||||
|
||||
const callId = Date.now().toString() + Math.random().toString(36).substr(2, 9)
|
||||
|
||||
currentCall.value = {
|
||||
callId: callId,
|
||||
peerId: peerId,
|
||||
callType: type
|
||||
callType: type,
|
||||
startTime: Date.now(),
|
||||
status: 'calling'
|
||||
}
|
||||
|
||||
callStatus.value = "calling"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = "等待对方接听..."
|
||||
|
||||
// 修复参数传递问题
|
||||
// 设置30秒超时(无人接听)
|
||||
const timeoutId = setTimeout(() => {
|
||||
if (callStatus.value === "calling") {
|
||||
callStatus.value = "ended"
|
||||
callError.value = "no-answer"
|
||||
callConnectionStatus.value = "对方无人接听"
|
||||
console.log("呼叫超时,对方无人接听")
|
||||
|
||||
// 发送无人接听信号
|
||||
sendMessage({
|
||||
senderId: userStore.currentUser?.id,
|
||||
receiverId: peerId,
|
||||
type: type === "video" ? "video-call" : "audio-call",
|
||||
callId: callId,
|
||||
callStatus: "no-answer"
|
||||
})
|
||||
|
||||
// 更新好友最后消息为未接来电
|
||||
friend.lastMessage = "[未接来电]"
|
||||
chatDB.updateFriendLastMessage(friend.id, userStore.currentUser.id, "[未接来电]", friend.unreadCount)
|
||||
|
||||
// 5秒后清除状态
|
||||
setTimeout(() => {
|
||||
callStatus.value = "idle"
|
||||
currentCall.value = null
|
||||
callConnectionStatus.value = ""
|
||||
}, 5000)
|
||||
}
|
||||
}, 30000)
|
||||
|
||||
currentCall.value.timeoutId = timeoutId
|
||||
|
||||
// 发送通话请求
|
||||
sendMessage({
|
||||
senderId: userStore.currentUser?.id,
|
||||
receiverId: peerId,
|
||||
@@ -295,7 +433,12 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const acceptCall = () => {
|
||||
if (!incomingCall.value) return
|
||||
|
||||
// 修复参数传递问题
|
||||
// 清除可能存在的超时计时器
|
||||
if (currentCall.value?.timeoutId) {
|
||||
clearTimeout(currentCall.value.timeoutId)
|
||||
}
|
||||
|
||||
// 发送接受信号
|
||||
sendMessage({
|
||||
senderId: userStore.currentUser?.id,
|
||||
receiverId: incomingCall.value.callerId,
|
||||
@@ -307,11 +450,15 @@ export const useChatStore = defineStore("chat", () => {
|
||||
currentCall.value = {
|
||||
callId: incomingCall.value.callId,
|
||||
peerId: incomingCall.value.callerId,
|
||||
callType: incomingCall.value.callType
|
||||
callType: incomingCall.value.callType,
|
||||
startTime: Date.now(),
|
||||
status: 'ongoing'
|
||||
}
|
||||
|
||||
callStatus.value = "ongoing"
|
||||
callConnectionStatus.value = "通话中"
|
||||
incomingCall.value = null
|
||||
callError.value = null
|
||||
console.log("接听通话")
|
||||
}
|
||||
|
||||
@@ -319,7 +466,7 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const rejectCall = () => {
|
||||
if (!incomingCall.value) return
|
||||
|
||||
// 修复参数传递问题
|
||||
// 发送拒绝信号
|
||||
sendMessage({
|
||||
senderId: userStore.currentUser?.id,
|
||||
receiverId: incomingCall.value.callerId,
|
||||
@@ -328,8 +475,26 @@ export const useChatStore = defineStore("chat", () => {
|
||||
callStatus: "rejected"
|
||||
})
|
||||
|
||||
callStatus.value = "idle"
|
||||
callStatus.value = "ended"
|
||||
callConnectionStatus.value = "已拒绝"
|
||||
callError.value = "rejected"
|
||||
|
||||
// 更新好友最后消息为已拒绝
|
||||
const friend = friends.value.find(f => f.id === incomingCall.value.callerId)
|
||||
if (friend) {
|
||||
friend.lastMessage = "[已拒绝]"
|
||||
chatDB.updateFriendLastMessage(friend.id, userStore.currentUser.id, "[已拒绝]", friend.unreadCount)
|
||||
}
|
||||
|
||||
incomingCall.value = null
|
||||
|
||||
// 5秒后清除状态
|
||||
setTimeout(() => {
|
||||
callStatus.value = "idle"
|
||||
callError.value = null
|
||||
callConnectionStatus.value = ""
|
||||
}, 5000)
|
||||
|
||||
console.log("拒绝通话")
|
||||
}
|
||||
|
||||
@@ -337,22 +502,35 @@ export const useChatStore = defineStore("chat", () => {
|
||||
const endCall = () => {
|
||||
const callId = currentCall.value?.callId || incomingCall.value?.callId
|
||||
const peerId = currentCall.value?.peerId || incomingCall.value?.callerId
|
||||
const callType = currentCall.value?.callType || incomingCall.value?.callType
|
||||
const callTypeVal = currentCall.value?.callType || incomingCall.value?.callType
|
||||
|
||||
if (!callId || !peerId) return
|
||||
|
||||
// 修复参数传递问题
|
||||
// 清除超时计时器
|
||||
if (currentCall.value?.timeoutId) {
|
||||
clearTimeout(currentCall.value.timeoutId)
|
||||
}
|
||||
|
||||
// 发送结束信号
|
||||
sendMessage({
|
||||
senderId: userStore.currentUser?.id,
|
||||
receiverId: peerId,
|
||||
type: callType === "video" ? "video-call" : "audio-call",
|
||||
type: callTypeVal === "video" ? "video-call" : "audio-call",
|
||||
callId: callId,
|
||||
callStatus: "ended"
|
||||
})
|
||||
|
||||
callStatus.value = "idle"
|
||||
currentCall.value = null
|
||||
incomingCall.value = null
|
||||
callStatus.value = "ended"
|
||||
callConnectionStatus.value = "通话结束"
|
||||
// 5秒后清除状态
|
||||
setTimeout(() => {
|
||||
callStatus.value = "idle"
|
||||
currentCall.value = null
|
||||
incomingCall.value = null
|
||||
callError.value = null
|
||||
callConnectionStatus.value = ""
|
||||
}, 5000)
|
||||
|
||||
console.log("结束通话")
|
||||
}
|
||||
|
||||
@@ -365,6 +543,8 @@ export const useChatStore = defineStore("chat", () => {
|
||||
currentCall,
|
||||
incomingCall,
|
||||
callStatus,
|
||||
callError,
|
||||
callConnectionStatus,
|
||||
isConnected,
|
||||
isInCall,
|
||||
isCalling,
|
||||
|
||||
Reference in New Issue
Block a user