一些方法

This commit is contained in:
2025-07-05 16:09:14 +08:00
parent 9c9245ce63
commit 95ceff344f
10 changed files with 876 additions and 252 deletions

View File

@@ -25,7 +25,7 @@ export function useWebSocket() {
socket.value.onopen = handleOpen
socket.value.onmessage = handleMessage
socket.value.onclose = disconnectWebSocket
socket.value.onclose = handleClose
socket.value.onerror = handleError
} catch (error) {
console.error("WebSocket连接失败:", error)
@@ -42,24 +42,17 @@ export function useWebSocket() {
reconnectDelay.value = 1000
chatStore.connectionStatus = "connected"
chatStore.socket = socket.value
// 绑定当前用户
if (userStore.currentUser?.id) {
bindUser(userStore.currentUser.id)
}
}
const handleMessage = (event) => {
try {
const data = JSON.parse(event.data)
console.log("收到消息:", data)
// 处理客户端ID
if (data?.clientId) {
// console.log("收到客户端ID:", data.clientId)
// userStore.setClientId(data.clientId)
//
// // 绑定用户
// if (userStore.currentUser?.id) {
bindUser(userStore.currentUser.id)
// }
return
}
console.log("收到WebSocket消息:", data)
// 处理通话信令
if (data.call_id && data.call_status) {
@@ -76,7 +69,7 @@ export function useWebSocket() {
}
}
const disconnectWebSocket = (event) => {
const handleClose = (event) => {
console.log("WebSocket连接已关闭:", event.code, event.reason)
isConnected.value = false
isConnecting.value = false
@@ -89,7 +82,7 @@ export function useWebSocket() {
reconnectAttempts.value++
reconnectDelay.value = Math.min(reconnectDelay.value * 2, 30000)
console.log(`尝试重连 (${reconnectAttempts.value}/${maxReconnectAttempts})`)
connect()
connectWebSocket()
}, reconnectDelay.value)
}
}
@@ -102,40 +95,15 @@ export function useWebSocket() {
const handleCallSignal = (data) => {
console.log("收到通话信令:", data)
// 根据通话状态处理
switch (data.call_status) {
case "invite":
// 显示来电通知
showIncomingCallDialog(data)
break
case "accepted":
// 通话被接受
handleCallAccepted(data)
break
case "rejected":
// 通话被拒绝
handleCallRejected(data)
break
case "ended":
// 通话结束
handleCallEnded(data)
break
case "candidate":
// ICE候选
handleIceCandidate(data)
break
}
chatStore.handleCallSignal(data)
}
const bindUser = (userId) => {
console.log(userId, 'ssssssssss')
if (!isConnected.value || !userId) return
const bindMessage = {
request_type: "bind",
sender_user_id: userId,
client_id: userStore.clientId,
}
send(bindMessage)
@@ -149,6 +117,7 @@ export function useWebSocket() {
try {
socket.value.send(JSON.stringify(message))
console.log("发送消息:", message)
return true
} catch (error) {
console.error("发送消息失败:", error)
@@ -169,7 +138,11 @@ export function useWebSocket() {
}
const sendCallSignal = (signal) => {
return send(signal)
return send({
request_type: "call_signal",
...signal,
sender_user_id: userStore.currentUser?.id
})
}
const disconnect = () => {
@@ -193,7 +166,7 @@ export function useWebSocket() {
})
onUnmounted(() => {
disconnectWebSocket()
disconnect()
})
return {
@@ -201,48 +174,11 @@ export function useWebSocket() {
isConnected,
isConnecting,
connectWebSocket,
disconnectWebSocket,
disconnect,
send,
sendMessage,
sendCallSignal,
bindUser,
generateCallId,
}
}
// 全局通话处理函数
const incomingCallDialog = null
const currentCall = null
function showIncomingCallDialog(callData) {
// 这里应该显示来电弹窗
console.log("显示来电弹窗:", callData)
// 创建来电通知
if ("Notification" in window && Notification.permission === "granted") {
new Notification(`${callData.sender_user_id} 邀请您进行${callData.message_type === 6 ? "视频" : "语音"}通话`, {
icon: "/favicon.ico",
tag: "incoming-call",
})
}
}
function handleCallAccepted(data) {
console.log("通话被接受:", data)
// 开始WebRTC连接
}
function handleCallRejected(data) {
console.log("通话被拒绝:", data)
// 显示拒绝消息
}
function handleCallEnded(data) {
console.log("通话结束:", data)
// 清理通话状态
}
function handleIceCandidate(data) {
console.log("收到ICE候选:", data)
// 处理ICE候选
}
}