Files
video_socket-server/client/public/call/connectview.js

212 lines
6.6 KiB
JavaScript
Raw Permalink Normal View History

2026-05-25 20:37:36 +08:00
import { showNotification } from '../shared/utils.js';
2026-05-16 21:26:19 +08:00
import store from './store.js';
2026-05-24 12:43:16 +08:00
import {
fetchConnectionDirectory,
fetchOnlineUsers,
renderConnectionIds,
renderOnlineUsers
2026-05-25 20:37:36 +08:00
} from './signaling/connect-directory.js';
import { createProfileSettingsController } from './controllers/profile-settings.js';
import { createLogger } from '../shared/logger.js';
2026-05-24 14:16:28 +08:00
const logger = createLogger('connectview');
2026-05-16 21:26:19 +08:00
let onWsStatusChange = null;
2026-05-16 22:22:34 +08:00
let cachedOnlineUsers = [];
2026-05-16 21:26:19 +08:00
2026-05-24 13:03:22 +08:00
const profileSettingsController = createProfileSettingsController({
store,
notify: showNotification
});
2026-05-16 21:26:19 +08:00
export function setWsStatusCallback(callback) {
onWsStatusChange = callback;
}
export function updateWsStatus(connected) {
const wsStatusDot = document.getElementById('wsStatusDot');
const wsStatusText = document.getElementById('wsStatusText');
2026-05-24 13:03:22 +08:00
2026-05-16 21:26:19 +08:00
if (wsStatusDot && wsStatusText) {
if (connected) {
wsStatusDot.className = 'w-2 h-2 bg-green-500 rounded-full animate-pulse';
wsStatusText.textContent = 'WebSocket已连接';
} else {
wsStatusDot.className = 'w-2 h-2 bg-gray-500 rounded-full';
wsStatusText.textContent = '未连接';
}
}
2026-05-24 13:03:22 +08:00
2026-05-16 21:26:19 +08:00
if (onWsStatusChange) {
onWsStatusChange(connected);
}
}
export async function initWebSocket() {
try {
await store.connectSignaling();
2026-05-16 23:07:08 +08:00
store.syncSocketUserInfo();
2026-05-16 21:26:19 +08:00
updateWsStatus(true);
2026-05-16 23:07:08 +08:00
await refreshOnlineUsers();
2026-05-24 14:16:28 +08:00
logger.debug('WebSocket initialized from connectview');
2026-05-16 21:26:19 +08:00
} catch (error) {
2026-05-24 14:16:28 +08:00
logger.error('Failed to initialize WebSocket:', error);
2026-05-16 21:26:19 +08:00
updateWsStatus(false);
showNotification('WebSocket连接失败请刷新页面重试', 'error');
}
}
2026-05-16 23:07:08 +08:00
async function refreshOnlineUsers(silent = true) {
try {
2026-05-24 12:43:16 +08:00
cachedOnlineUsers = await fetchOnlineUsers();
updateOnlineUsersList(cachedOnlineUsers);
2026-05-24 13:03:22 +08:00
2026-05-16 23:07:08 +08:00
if (!silent) {
showNotification(`当前共有 ${cachedOnlineUsers.length} 个WebSocket用户在线`);
}
} catch (error) {
2026-05-24 14:16:28 +08:00
logger.error('Error fetching online users:', error);
2026-05-16 23:07:08 +08:00
if (!silent) {
showNotification('获取在线用户失败', 'error');
}
}
}
2026-05-16 21:26:19 +08:00
async function getAllConnectionIds() {
2026-05-16 23:07:08 +08:00
showNotification('正在获取连接ID和在线用户...');
2026-05-24 13:03:22 +08:00
2026-05-16 21:26:19 +08:00
try {
2026-05-24 12:43:16 +08:00
const { connectionIds, users } = await fetchConnectionDirectory();
cachedOnlineUsers = users;
updateConnectionIdList(connectionIds);
updateOnlineUsersList(cachedOnlineUsers);
2026-05-16 21:26:19 +08:00
} catch (error) {
2026-05-24 14:16:28 +08:00
logger.error('Error fetching connection IDs:', error);
2026-05-16 22:22:34 +08:00
showNotification('获取连接信息失败', 'error');
2026-05-16 21:26:19 +08:00
}
}
2026-05-24 12:43:16 +08:00
function updateConnectionIdList(connectionIds) {
2026-05-16 21:26:19 +08:00
const idsContainer = document.getElementById('idsContainer');
const connectionIdsList = document.getElementById('connectionIdsList');
2026-05-24 12:43:16 +08:00
renderConnectionIds({
connectionIds,
idsContainer,
connectionIdsList,
onSelectConnectionId: selectConnectionId
});
2026-05-16 21:26:19 +08:00
2026-05-24 12:43:16 +08:00
if (idsContainer) {
2026-05-16 21:26:19 +08:00
showNotification(`找到 ${connectionIds.length} 个连接ID`);
}
}
2026-05-18 23:03:28 +08:00
function getCurrentUserId() {
try {
const settings = JSON.parse(localStorage.getItem('userSettings') || '{}');
return settings.userId || settings.id || '';
} catch (error) {
2026-05-24 14:16:28 +08:00
logger.error('Error parsing current user settings:', error);
2026-05-18 23:03:28 +08:00
return '';
}
}
2026-05-24 12:43:16 +08:00
function updateOnlineUsersList(users) {
2026-05-16 22:22:34 +08:00
const onlineUsersList = document.getElementById('onlineUsersList');
const usersContainer = document.getElementById('usersContainer');
const onlineUsersSummary = document.getElementById('onlineUsersSummary');
2026-05-24 12:43:16 +08:00
renderOnlineUsers({
users,
currentUserId: getCurrentUserId(),
onlineUsersList,
usersContainer,
onlineUsersSummary
2026-05-16 22:22:34 +08:00
});
}
2026-05-24 13:03:22 +08:00
function selectConnectionId(connectionId) {
2026-05-16 21:26:19 +08:00
const connectionIdInput = document.getElementById('connectionIdInput');
if (connectionIdInput) {
2026-05-24 13:03:22 +08:00
connectionIdInput.value = connectionId;
showNotification(`已选择连接ID: ${connectionId}`);
2026-05-16 21:26:19 +08:00
}
}
export function loadUserSettings() {
2026-05-24 13:03:22 +08:00
profileSettingsController.loadUserSettings();
2026-05-16 21:26:19 +08:00
}
export function saveSettings() {
2026-05-24 13:03:22 +08:00
profileSettingsController.saveSettings();
2026-05-16 21:26:19 +08:00
}
export function handleAvatarUpload(event) {
2026-05-24 13:03:22 +08:00
profileSettingsController.handleAvatarUpload(event);
2026-05-16 21:26:19 +08:00
}
export function copyUserId() {
2026-05-24 13:03:22 +08:00
profileSettingsController.copyUserId();
2026-05-16 21:26:19 +08:00
}
export function toggleSettingsMenu() {
2026-05-24 13:03:22 +08:00
profileSettingsController.toggleSettingsMenu();
2026-05-16 21:26:19 +08:00
}
export function bindConnectViewEvents(onJoinCall, onCreateCall) {
const connectBtn = document.getElementById('connectBtn');
2026-05-24 13:03:22 +08:00
if (connectBtn && !connectBtn.dataset.bound) {
2026-05-16 21:26:19 +08:00
connectBtn.addEventListener('click', () => {
const connectionIdInput = document.getElementById('connectionIdInput');
const connectionId = connectionIdInput ? connectionIdInput.value.trim() : '';
2026-05-24 13:03:22 +08:00
2026-05-16 21:26:19 +08:00
if (connectionId) {
onJoinCall(connectionId);
} else {
showNotification('请输入连接ID', 'error');
}
});
2026-05-24 13:03:22 +08:00
connectBtn.dataset.bound = 'true';
2026-05-16 21:26:19 +08:00
}
const createCallBtn = document.getElementById('createCallBtn');
2026-05-24 13:03:22 +08:00
if (createCallBtn && !createCallBtn.dataset.bound) {
2026-05-16 21:26:19 +08:00
createCallBtn.addEventListener('click', onCreateCall);
2026-05-24 13:03:22 +08:00
createCallBtn.dataset.bound = 'true';
2026-05-16 21:26:19 +08:00
}
const browseIdsBtn = document.getElementById('browseIdsBtn');
2026-05-24 13:03:22 +08:00
if (browseIdsBtn && !browseIdsBtn.dataset.bound) {
2026-05-16 21:26:19 +08:00
browseIdsBtn.addEventListener('click', getAllConnectionIds);
2026-05-24 13:03:22 +08:00
browseIdsBtn.dataset.bound = 'true';
2026-05-16 21:26:19 +08:00
}
const connectionIdInput = document.getElementById('connectionIdInput');
2026-05-24 13:03:22 +08:00
if (connectionIdInput && !connectionIdInput.dataset.bound) {
connectionIdInput.addEventListener('keypress', (event) => {
if (event.key !== 'Enter') {
return;
}
const connectionId = connectionIdInput.value.trim();
if (connectionId) {
onJoinCall(connectionId);
} else {
showNotification('请输入连接ID', 'error');
2026-05-16 21:26:19 +08:00
}
});
2026-05-24 13:03:22 +08:00
connectionIdInput.dataset.bound = 'true';
2026-05-16 21:26:19 +08:00
}
const userSettingsBtn = document.getElementById('userSettingsBtn');
2026-05-24 13:03:22 +08:00
if (userSettingsBtn && !userSettingsBtn.dataset.bound) {
2026-05-16 21:26:19 +08:00
userSettingsBtn.addEventListener('click', toggleSettingsMenu);
2026-05-24 13:03:22 +08:00
userSettingsBtn.dataset.bound = 'true';
2026-05-16 21:26:19 +08:00
}
}
2026-05-24 13:03:22 +08:00
profileSettingsController.bindDocumentEvents();
profileSettingsController.bindWindowHandlers();
2026-05-16 21:26:19 +08:00
window.selectConnectionId = selectConnectionId;