消息模块开发完成
This commit is contained in:
@@ -16,6 +16,8 @@ import {
|
||||
let connectionId = "";
|
||||
// 当前视图状态:'connect' 或 'call'(可用于未来扩展)
|
||||
let currentView = 'connect';
|
||||
let pendingIncomingInvite = null;
|
||||
let inviteHandlersBound = false;
|
||||
|
||||
function getInvitePayloadFromUrl() {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
@@ -39,6 +41,7 @@ function showCallRequestDialog(caller = {}) {
|
||||
const callerName = caller.name || '邀请方';
|
||||
const callerAvatar = caller.avatar || '/images/p2.png';
|
||||
const targetConnectionId = caller.connectionId || '';
|
||||
pendingIncomingInvite = caller;
|
||||
|
||||
if (document.getElementById('callRequestName')) {
|
||||
document.getElementById('callRequestName').textContent = callerName;
|
||||
@@ -60,6 +63,44 @@ function showCallRequestDialog(caller = {}) {
|
||||
dialog.classList.remove('hidden');
|
||||
}
|
||||
|
||||
function getCurrentUserProfile() {
|
||||
try {
|
||||
const settings = JSON.parse(localStorage.getItem('userSettings') || '{}');
|
||||
return {
|
||||
userId: settings.userId || settings.id || '',
|
||||
name: settings.name || '我',
|
||||
avatar: settings.avatar || '/images/p1.png'
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error parsing user settings:', error);
|
||||
return {
|
||||
userId: '',
|
||||
name: '我',
|
||||
avatar: '/images/p1.png'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function bindInviteSignalHandlers() {
|
||||
if (inviteHandlersBound) {
|
||||
return;
|
||||
}
|
||||
|
||||
store.onSocketEvent('invite-call', (payload) => {
|
||||
pendingIncomingInvite = {
|
||||
connectionId: payload.connectionId,
|
||||
inviterSocketId: payload.inviterSocketId,
|
||||
inviterUserId: payload.inviterUserId,
|
||||
name: payload.inviterName || '邀请方',
|
||||
avatar: payload.inviterAvatar || '/images/p2.png'
|
||||
};
|
||||
showCallRequestDialog(pendingIncomingInvite);
|
||||
showNotification(`${pendingIncomingInvite.name} 邀请你加入通话`);
|
||||
});
|
||||
|
||||
inviteHandlersBound = true;
|
||||
}
|
||||
|
||||
function bindInviteDialogEvents() {
|
||||
window.showCallRequest = function (caller) {
|
||||
showCallRequestDialog(caller);
|
||||
@@ -70,6 +111,18 @@ function bindInviteDialogEvents() {
|
||||
if (dialog) {
|
||||
dialog.classList.add('hidden');
|
||||
}
|
||||
if (pendingIncomingInvite) {
|
||||
try {
|
||||
store.sendInviteRejected({
|
||||
connectionId: pendingIncomingInvite.connectionId || '',
|
||||
targetSocketId: pendingIncomingInvite.inviterSocketId || '',
|
||||
targetUserId: pendingIncomingInvite.inviterUserId || ''
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error rejecting invite:', error);
|
||||
}
|
||||
}
|
||||
pendingIncomingInvite = null;
|
||||
showNotification('已拒绝通话请求');
|
||||
};
|
||||
|
||||
@@ -80,6 +133,7 @@ function bindInviteDialogEvents() {
|
||||
}
|
||||
|
||||
const targetConnectionId =
|
||||
(pendingIncomingInvite && pendingIncomingInvite.connectionId) ||
|
||||
connectionId ||
|
||||
localStorage.getItem('connectionId') ||
|
||||
new URLSearchParams(window.location.search).get('connectionId');
|
||||
@@ -91,7 +145,23 @@ function bindInviteDialogEvents() {
|
||||
|
||||
connectionId = targetConnectionId;
|
||||
localStorage.setItem('connectionId', targetConnectionId);
|
||||
|
||||
if (pendingIncomingInvite) {
|
||||
try {
|
||||
store.sendInviteAccepted({
|
||||
connectionId: targetConnectionId,
|
||||
targetSocketId: pendingIncomingInvite.inviterSocketId || '',
|
||||
targetUserId: pendingIncomingInvite.inviterUserId || ''
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error accepting invite:', error);
|
||||
showNotification('接受邀请失败,请稍后重试', 'error');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
showNotification('已接受通话请求');
|
||||
pendingIncomingInvite = null;
|
||||
|
||||
if (currentView !== 'call') {
|
||||
await switchToCallView(targetConnectionId);
|
||||
@@ -139,9 +209,11 @@ async function switchToCallView(connectionId) {
|
||||
bindCallViewDomEvents();
|
||||
|
||||
console.log('Video call app initialized successfully');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error initializing app:', error);
|
||||
showNotification('初始化失败,请刷新页面重试', 'error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,14 +249,14 @@ function bindCallViewDomEvents() {
|
||||
};
|
||||
|
||||
// 切换麦克风
|
||||
window.toggleMute = function (button) {
|
||||
window.toggleMute = function () {
|
||||
const state = store.getState();
|
||||
const currentState = state.session.localUser.mediaState.audio;
|
||||
store.updateLocalMedia('audio', !currentState);
|
||||
};
|
||||
|
||||
// 切换视频
|
||||
window.toggleVideo = function (button) {
|
||||
window.toggleVideo = function () {
|
||||
const state = store.getState();
|
||||
const currentState = state.session.localUser.mediaState.video;
|
||||
store.updateLocalMedia('video', !currentState);
|
||||
@@ -196,7 +268,7 @@ function bindCallViewDomEvents() {
|
||||
};
|
||||
|
||||
// 切换录屏
|
||||
window.toggleRecording = function (button) {
|
||||
window.toggleRecording = function () {
|
||||
const state = store.getState();
|
||||
const currentState = state.session.localUser.mediaState.recording || false;
|
||||
store.updateLocalMedia('recording', !currentState);
|
||||
@@ -304,6 +376,7 @@ window.addEventListener('DOMContentLoaded', async () => {
|
||||
|
||||
// 初始化WebSocket连接(在connect视图就建立WebSocket)
|
||||
await initWebSocket();
|
||||
bindInviteSignalHandlers();
|
||||
|
||||
// 绑定connect视图事件(加入通话、创建通话等)
|
||||
bindConnectViewEvents(handleJoinCall, handleCreateCall);
|
||||
|
||||
Reference in New Issue
Block a user