|
|
<!DOCTYPE html> |
|
|
<html lang="zh-CN"> |
|
|
<head> |
|
|
<meta charset="UTF-8"> |
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
|
|
<title>视频会议应用</title> |
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.4.1/socket.io.min.js"></script> |
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/webrtc-adapter/8.1.1/adapter.min.js"></script> |
|
|
<style> |
|
|
* { |
|
|
margin: 0; |
|
|
padding: 0; |
|
|
box-sizing: border-box; |
|
|
font-family: 'Arial', sans-serif; |
|
|
} |
|
|
|
|
|
body { |
|
|
background-color: #f0f2f5; |
|
|
color: #333; |
|
|
} |
|
|
|
|
|
.container { |
|
|
max-width: 1200px; |
|
|
margin: 0 auto; |
|
|
padding: 20px; |
|
|
} |
|
|
|
|
|
header { |
|
|
display: flex; |
|
|
justify-content: space-between; |
|
|
align-items: center; |
|
|
padding: 15px 0; |
|
|
border-bottom: 1px solid #ddd; |
|
|
margin-bottom: 20px; |
|
|
} |
|
|
|
|
|
.logo { |
|
|
font-size: 24px; |
|
|
font-weight: bold; |
|
|
color: #0066cc; |
|
|
} |
|
|
|
|
|
.controls { |
|
|
display: flex; |
|
|
gap: 10px; |
|
|
} |
|
|
|
|
|
button { |
|
|
padding: 8px 16px; |
|
|
background-color: #0066cc; |
|
|
color: white; |
|
|
border: none; |
|
|
border-radius: 4px; |
|
|
cursor: pointer; |
|
|
font-size: 14px; |
|
|
transition: background-color 0.3s; |
|
|
} |
|
|
|
|
|
button:hover { |
|
|
background-color: #0055aa; |
|
|
} |
|
|
|
|
|
button.red { |
|
|
background-color: #cc0000; |
|
|
} |
|
|
|
|
|
button.red:hover { |
|
|
background-color: #aa0000; |
|
|
} |
|
|
|
|
|
.disabled { |
|
|
background-color: #888; |
|
|
cursor: not-allowed; |
|
|
} |
|
|
|
|
|
.disabled:hover { |
|
|
background-color: #888; |
|
|
} |
|
|
|
|
|
.meeting-container { |
|
|
display: grid; |
|
|
grid-template-columns: 1fr 300px; |
|
|
gap: 20px; |
|
|
height: calc(100vh - 150px); |
|
|
} |
|
|
|
|
|
.video-grid { |
|
|
display: grid; |
|
|
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); |
|
|
grid-auto-rows: 1fr; |
|
|
gap: 15px; |
|
|
height: 100%; |
|
|
overflow: auto; |
|
|
} |
|
|
|
|
|
.video-container { |
|
|
background-color: #222; |
|
|
border-radius: 8px; |
|
|
overflow: hidden; |
|
|
position: relative; |
|
|
} |
|
|
|
|
|
.video-container video { |
|
|
width: 100%; |
|
|
height: 100%; |
|
|
object-fit: cover; |
|
|
} |
|
|
|
|
|
.user-name { |
|
|
position: absolute; |
|
|
bottom: 10px; |
|
|
left: 10px; |
|
|
color: white; |
|
|
background-color: rgba(0, 0, 0, 0.5); |
|
|
padding: 4px 8px; |
|
|
border-radius: 4px; |
|
|
font-size: 14px; |
|
|
} |
|
|
|
|
|
.mic-status { |
|
|
position: absolute; |
|
|
top: 10px; |
|
|
right: 10px; |
|
|
color: white; |
|
|
padding: 4px; |
|
|
border-radius: 50%; |
|
|
} |
|
|
|
|
|
.sidebar { |
|
|
background-color: white; |
|
|
border-radius: 8px; |
|
|
display: flex; |
|
|
flex-direction: column; |
|
|
height: 100%; |
|
|
} |
|
|
|
|
|
.tabs { |
|
|
display: flex; |
|
|
border-bottom: 1px solid #ddd; |
|
|
} |
|
|
|
|
|
.tab { |
|
|
padding: 10px 15px; |
|
|
cursor: pointer; |
|
|
} |
|
|
|
|
|
.tab.active { |
|
|
color: #0066cc; |
|
|
border-bottom: 2px solid #0066cc; |
|
|
} |
|
|
|
|
|
.tab-content { |
|
|
flex-grow: 1; |
|
|
overflow: auto; |
|
|
padding: 15px; |
|
|
} |
|
|
|
|
|
.participants-list { |
|
|
list-style: none; |
|
|
} |
|
|
|
|
|
.participant { |
|
|
display: flex; |
|
|
align-items: center; |
|
|
padding: 8px 0; |
|
|
border-bottom: 1px solid #eee; |
|
|
} |
|
|
|
|
|
.participant-info { |
|
|
flex-grow: 1; |
|
|
} |
|
|
|
|
|
.chat-container { |
|
|
display: flex; |
|
|
flex-direction: column; |
|
|
height: 100%; |
|
|
} |
|
|
|
|
|
.messages { |
|
|
flex-grow: 1; |
|
|
overflow: auto; |
|
|
padding: 10px; |
|
|
} |
|
|
|
|
|
.message { |
|
|
margin-bottom: 10px; |
|
|
padding: 8px 12px; |
|
|
border-radius: 6px; |
|
|
max-width: 80%; |
|
|
} |
|
|
|
|
|
.message.received { |
|
|
background-color: #f1f1f1; |
|
|
align-self: flex-start; |
|
|
} |
|
|
|
|
|
.message.sent { |
|
|
background-color: #dcf8c6; |
|
|
align-self: flex-end; |
|
|
} |
|
|
|
|
|
.sender { |
|
|
font-size: 12px; |
|
|
color: #555; |
|
|
margin-bottom: 2px; |
|
|
} |
|
|
|
|
|
.chat-input { |
|
|
display: flex; |
|
|
padding: 10px; |
|
|
border-top: 1px solid #ddd; |
|
|
} |
|
|
|
|
|
.chat-input input { |
|
|
flex-grow: 1; |
|
|
padding: 8px 12px; |
|
|
border: 1px solid #ddd; |
|
|
border-radius: 4px; |
|
|
margin-right: 10px; |
|
|
outline: none; |
|
|
} |
|
|
|
|
|
.join-form { |
|
|
max-width: 400px; |
|
|
margin: 100px auto; |
|
|
background-color: white; |
|
|
padding: 30px; |
|
|
border-radius: 8px; |
|
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); |
|
|
} |
|
|
|
|
|
.form-group { |
|
|
margin-bottom: 20px; |
|
|
} |
|
|
|
|
|
label { |
|
|
display: block; |
|
|
margin-bottom: 8px; |
|
|
font-weight: bold; |
|
|
} |
|
|
|
|
|
input[type="text"], |
|
|
input[type="password"] { |
|
|
width: 100%; |
|
|
padding: 10px; |
|
|
border: 1px solid #ddd; |
|
|
border-radius: 4px; |
|
|
font-size: 16px; |
|
|
} |
|
|
|
|
|
.button-group { |
|
|
display: flex; |
|
|
justify-content: space-between; |
|
|
} |
|
|
|
|
|
.hidden { |
|
|
display: none; |
|
|
} |
|
|
|
|
|
|
|
|
.screen-share { |
|
|
grid-column: span 2; |
|
|
height: 100%; |
|
|
background-color: #222; |
|
|
border-radius: 8px; |
|
|
overflow: hidden; |
|
|
position: relative; |
|
|
} |
|
|
|
|
|
.screen-share video { |
|
|
width: 100%; |
|
|
height: 100%; |
|
|
object-fit: contain; |
|
|
} |
|
|
|
|
|
.screen-share-label { |
|
|
position: absolute; |
|
|
top: 10px; |
|
|
left: 10px; |
|
|
color: white; |
|
|
background-color: rgba(0, 0, 0, 0.5); |
|
|
padding: 4px 8px; |
|
|
border-radius: 4px; |
|
|
font-size: 14px; |
|
|
} |
|
|
|
|
|
|
|
|
@media (max-width: 768px) { |
|
|
.meeting-container { |
|
|
grid-template-columns: 1fr; |
|
|
} |
|
|
|
|
|
.sidebar { |
|
|
margin-top: 20px; |
|
|
height: 300px; |
|
|
} |
|
|
} |
|
|
</style> |
|
|
</head> |
|
|
<body> |
|
|
|
|
|
<div class="join-form" id="joinForm"> |
|
|
<h2 style="text-align: center; margin-bottom: 20px;">加入会议</h2> |
|
|
<div class="form-group"> |
|
|
<label for="displayName">您的姓名</label> |
|
|
<input type="text" id="displayName" placeholder="请输入您的姓名"> |
|
|
</div> |
|
|
<div class="form-group"> |
|
|
<label for="roomId">会议 ID</label> |
|
|
<input type="text" id="roomId" placeholder="请输入会议 ID 或创建新会议"> |
|
|
</div> |
|
|
<div class="button-group"> |
|
|
<button id="createMeeting">创建会议</button> |
|
|
<button id="joinMeeting">加入会议</button> |
|
|
</div> |
|
|
</div> |
|
|
|
|
|
|
|
|
<div class="container hidden" id="meetingContainer"> |
|
|
<header> |
|
|
<div class="logo">视频会议</div> |
|
|
<div class="meeting-info"> |
|
|
会议 ID: <span id="currentRoomId"></span> |
|
|
<button id="copyRoomId">复制</button> |
|
|
</div> |
|
|
<div class="controls"> |
|
|
<button id="toggleAudio"><i class="fas fa-microphone"></i> 麦克风</button> |
|
|
<button id="toggleVideo"><i class="fas fa-video"></i> 摄像头</button> |
|
|
<button id="toggleScreen"><i class="fas fa-desktop"></i> 屏幕共享</button> |
|
|
<button id="leaveCall" class="red"><i class="fas fa-phone-slash"></i> 离开会议</button> |
|
|
</div> |
|
|
</header> |
|
|
|
|
|
<div class="meeting-container"> |
|
|
<div class="video-grid" id="videoGrid"> |
|
|
|
|
|
</div> |
|
|
|
|
|
<div class="sidebar"> |
|
|
<div class="tabs"> |
|
|
<div class="tab active" data-tab="participants">参会者</div> |
|
|
<div class="tab" data-tab="chat">聊天</div> |
|
|
</div> |
|
|
|
|
|
<div class="tab-content" id="participantsTab"> |
|
|
<ul class="participants-list" id="participantsList"> |
|
|
|
|
|
</ul> |
|
|
</div> |
|
|
|
|
|
<div class="tab-content hidden" id="chatTab"> |
|
|
<div class="chat-container"> |
|
|
<div class="messages" id="messages"> |
|
|
|
|
|
</div> |
|
|
<div class="chat-input"> |
|
|
<input type="text" id="messageInput" placeholder="输入消息..."> |
|
|
<button id="sendMessage">发送</button> |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
</div> |
|
|
|
|
|
<script> |
|
|
|
|
|
const joinForm = document.getElementById('joinForm'); |
|
|
const meetingContainer = document.getElementById('meetingContainer'); |
|
|
const displayNameInput = document.getElementById('displayName'); |
|
|
const roomIdInput = document.getElementById('roomId'); |
|
|
const createMeetingBtn = document.getElementById('createMeeting'); |
|
|
const joinMeetingBtn = document.getElementById('joinMeeting'); |
|
|
const currentRoomIdSpan = document.getElementById('currentRoomId'); |
|
|
const copyRoomIdBtn = document.getElementById('copyRoomId'); |
|
|
const toggleAudioBtn = document.getElementById('toggleAudio'); |
|
|
const toggleVideoBtn = document.getElementById('toggleVideo'); |
|
|
const toggleScreenBtn = document.getElementById('toggleScreen'); |
|
|
const leaveCallBtn = document.getElementById('leaveCall'); |
|
|
const videoGrid = document.getElementById('videoGrid'); |
|
|
const tabs = document.querySelectorAll('.tab'); |
|
|
const tabContents = document.querySelectorAll('.tab-content'); |
|
|
const participantsList = document.getElementById('participantsList'); |
|
|
const messages = document.getElementById('messages'); |
|
|
const messageInput = document.getElementById('messageInput'); |
|
|
const sendMessageBtn = document.getElementById('sendMessage'); |
|
|
|
|
|
|
|
|
let currentRoom = null; |
|
|
let localStream = null; |
|
|
let screenStream = null; |
|
|
let peerConnections = {}; |
|
|
let dataChannels = {}; |
|
|
let localAudioEnabled = true; |
|
|
let localVideoEnabled = true; |
|
|
|
|
|
|
|
|
const mockServer = { |
|
|
connections: {}, |
|
|
|
|
|
connect(roomId, userId) { |
|
|
if (!this.connections[roomId]) { |
|
|
this.connections[roomId] = {}; |
|
|
} |
|
|
this.connections[roomId][userId] = { |
|
|
onOffer: null, |
|
|
onAnswer: null, |
|
|
onIceCandidate: null, |
|
|
sendOffer: (targetId, offer) => { |
|
|
const target = this.connections[roomId][targetId]; |
|
|
if (target && target.onOffer) { |
|
|
setTimeout(() => { |
|
|
target.onOffer(userId, offer); |
|
|
}, 100); |
|
|
} |
|
|
}, |
|
|
sendAnswer: (targetId, answer) => { |
|
|
const target = this.connections[roomId][targetId]; |
|
|
if (target && target.onAnswer) { |
|
|
setTimeout(() => { |
|
|
target.onAnswer(userId, answer); |
|
|
}, 100); |
|
|
} |
|
|
}, |
|
|
sendIceCandidate: (targetId, candidate) => { |
|
|
const target = this.connections[roomId][targetId]; |
|
|
if (target && target.onIceCandidate) { |
|
|
setTimeout(() => { |
|
|
target.onIceCandidate(userId, candidate); |
|
|
}, 100); |
|
|
} |
|
|
}, |
|
|
sendMessage: (targetId, message) => { |
|
|
const target = this.connections[roomId][targetId]; |
|
|
if (target && target.onMessage) { |
|
|
setTimeout(() => { |
|
|
target.onMessage(userId, message); |
|
|
}, 100); |
|
|
} |
|
|
}, |
|
|
onMessage: null |
|
|
}; |
|
|
|
|
|
return { |
|
|
userId, |
|
|
peers: Object.keys(this.connections[roomId]).filter(id => id !== userId) |
|
|
}; |
|
|
}, |
|
|
|
|
|
disconnect(roomId, userId) { |
|
|
if (this.connections[roomId]) { |
|
|
delete this.connections[roomId][userId]; |
|
|
if (Object.keys(this.connections[roomId]).length === 0) { |
|
|
delete this.connections[roomId]; |
|
|
} |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
function generateId() { |
|
|
return Math.random().toString(36).substr(2, 9); |
|
|
} |
|
|
|
|
|
|
|
|
async function initLocalStream() { |
|
|
try { |
|
|
localStream = await navigator.mediaDevices.getUserMedia({ |
|
|
audio: true, |
|
|
video: true |
|
|
}); |
|
|
|
|
|
|
|
|
const videoContainer = document.createElement('div'); |
|
|
videoContainer.className = 'video-container'; |
|
|
videoContainer.id = 'local-video-container'; |
|
|
|
|
|
const video = document.createElement('video'); |
|
|
video.srcObject = localStream; |
|
|
video.autoplay = true; |
|
|
video.muted = true; |
|
|
video.playsInline = true; |
|
|
|
|
|
const userName = document.createElement('div'); |
|
|
userName.className = 'user-name'; |
|
|
userName.textContent = displayNameInput.value + ' (你)'; |
|
|
|
|
|
videoContainer.appendChild(video); |
|
|
videoContainer.appendChild(userName); |
|
|
|
|
|
videoGrid.appendChild(videoContainer); |
|
|
|
|
|
return true; |
|
|
} catch (error) { |
|
|
console.error('无法获取媒体流', error); |
|
|
alert('无法访问摄像头或麦克风,请确保它们已连接并授予访问权限。'); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async function joinMeeting() { |
|
|
const displayName = displayNameInput.value.trim(); |
|
|
let roomId = roomIdInput.value.trim(); |
|
|
|
|
|
if (!displayName) { |
|
|
alert('请输入您的姓名'); |
|
|
return; |
|
|
} |
|
|
|
|
|
if (!roomId) { |
|
|
alert('请输入会议 ID'); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const success = await initLocalStream(); |
|
|
if (!success) return; |
|
|
|
|
|
|
|
|
const userId = generateId(); |
|
|
const { peers } = mockServer.connect(roomId, userId); |
|
|
|
|
|
currentRoom = { |
|
|
id: roomId, |
|
|
userId, |
|
|
displayName |
|
|
}; |
|
|
|
|
|
|
|
|
currentRoomIdSpan.textContent = roomId; |
|
|
|
|
|
|
|
|
for (const peerId of peers) { |
|
|
createPeerConnection(peerId); |
|
|
} |
|
|
|
|
|
|
|
|
const connection = mockServer.connections[roomId][userId]; |
|
|
|
|
|
connection.onOffer = async (peerId, offer) => { |
|
|
const pc = peerConnections[peerId] || createPeerConnection(peerId); |
|
|
|
|
|
try { |
|
|
await pc.setRemoteDescription(new RTCSessionDescription(offer)); |
|
|
|
|
|
const answer = await pc.createAnswer(); |
|
|
await pc.setLocalDescription(answer); |
|
|
|
|
|
connection.sendAnswer(peerId, answer); |
|
|
} catch (error) { |
|
|
console.error('处理提议时出错', error); |
|
|
} |
|
|
}; |
|
|
|
|
|
connection.onAnswer = async (peerId, answer) => { |
|
|
const pc = peerConnections[peerId]; |
|
|
if (pc) { |
|
|
try { |
|
|
await pc.setRemoteDescription(new RTCSessionDescription(answer)); |
|
|
} catch (error) { |
|
|
console.error('处理应答时出错', error); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
connection.onIceCandidate = (peerId, candidate) => { |
|
|
const pc = peerConnections[peerId]; |
|
|
if (pc) { |
|
|
try { |
|
|
pc.addIceCandidate(new RTCIceCandidate(candidate)); |
|
|
} catch (error) { |
|
|
console.error('添加 ICE 候选时出错', error); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
connection.onMessage = (peerId, message) => { |
|
|
displayMessage(peerId, message); |
|
|
}; |
|
|
|
|
|
|
|
|
joinForm.classList.add('hidden'); |
|
|
meetingContainer.classList.remove('hidden'); |
|
|
} |
|
|
|
|
|
|
|
|
function createPeerConnection(peerId) { |
|
|
const pc = new RTCPeerConnection({ |
|
|
iceServers: [ |
|
|
{ urls: 'stun:stun.l.google.com:19302' }, |
|
|
{ urls: 'stun:stun1.l.google.com:19302' } |
|
|
] |
|
|
}); |
|
|
|
|
|
peerConnections[peerId] = pc; |
|
|
|
|
|
|
|
|
localStream.getTracks().forEach(track => { |
|
|
pc.addTrack(track, localStream); |
|
|
}); |
|
|
|
|
|
|
|
|
const dataChannel = pc.createDataChannel(`chat-${peerId}`); |
|
|
|
|
|
dataChannel.onopen = () => { |
|
|
console.log(`与 ${peerId} 的数据通道已打开`); |
|
|
dataChannels[peerId] = dataChannel; |
|
|
}; |
|
|
|
|
|
dataChannel.onclose = () => { |
|
|
console.log(`与 ${peerId} 的数据通道已关闭`); |
|
|
delete dataChannels[peerId]; |
|
|
}; |
|
|
|
|
|
|
|
|
pc.onicecandidate = (event) => { |
|
|
if (event.candidate) { |
|
|
const connection = mockServer.connections[currentRoom.id][currentRoom.userId]; |
|
|
connection.sendIceCandidate(peerId, event.candidate); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
pc.ontrack = (event) => { |
|
|
const stream = event.streams[0]; |
|
|
|
|
|
|
|
|
const existingContainer = document.getElementById(`peer-${peerId}-container`); |
|
|
|
|
|
if (!existingContainer) { |
|
|
|
|
|
const videoContainer = document.createElement('div'); |
|
|
videoContainer.className = 'video-container'; |
|
|
videoContainer.id = `peer-${peerId}-container`; |
|
|
|
|
|
const video = document.createElement('video'); |
|
|
video.srcObject = stream; |
|
|
video.autoplay = true; |
|
|
video.playsInline = true; |
|
|
|
|
|
const userName = document.createElement('div'); |
|
|
userName.className = 'user-name'; |
|
|
userName.textContent = '参会者'; |
|
|
|
|
|
videoContainer.appendChild(video); |
|
|
videoContainer.appendChild(userName); |
|
|
|
|
|
videoGrid.appendChild(videoContainer); |
|
|
|
|
|
|
|
|
const participant = document.createElement('li'); |
|
|
participant.className = 'participant'; |
|
|
participant.id = `participant-${peerId}`; |
|
|
|
|
|
const participantInfo = document.createElement('div'); |
|
|
participantInfo.className = 'participant-info'; |
|
|
participantInfo.textContent = '参会者'; |
|
|
|
|
|
participant.appendChild(participantInfo); |
|
|
participantsList.appendChild(participant); |
|
|
} else { |
|
|
|
|
|
const video = existingContainer.querySelector('video'); |
|
|
if (video.srcObject !== stream) { |
|
|
video.srcObject = stream; |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
pc.ondatachannel = (event) => { |
|
|
const receiveChannel = event.channel; |
|
|
|
|
|
receiveChannel.onmessage = (messageEvent) => { |
|
|
displayMessage(peerId, JSON.parse(messageEvent.data)); |
|
|
}; |
|
|
|
|
|
receiveChannel.onopen = () => { |
|
|
console.log(`接收到来自 ${peerId} 的数据通道`); |
|
|
dataChannels[peerId] = receiveChannel; |
|
|
}; |
|
|
|
|
|
receiveChannel.onclose = () => { |
|
|
console.log(`来自 ${peerId} 的数据通道已关闭`); |
|
|
delete dataChannels[peerId]; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
pc.createOffer() |
|
|
.then(offer => pc.setLocalDescription(offer)) |
|
|
.then(() => { |
|
|
const connection = mockServer.connections[currentRoom.id][currentRoom.userId]; |
|
|
connection.sendOffer(peerId, pc.localDescription); |
|
|
}) |
|
|
.catch(error => { |
|
|
console.error('创建提议时出错', error); |
|
|
}); |
|
|
|
|
|
return pc; |
|
|
} |
|
|
|
|
|
|
|
|
function toggleAudio() { |
|
|
const audioTracks = localStream.getAudioTracks(); |
|
|
|
|
|
if (audioTracks.length > 0) { |
|
|
const track = audioTracks[0]; |
|
|
track.enabled = !track.enabled; |
|
|
localAudioEnabled = track.enabled; |
|
|
|
|
|
toggleAudioBtn.innerHTML = localAudioEnabled ? |
|
|
'<i class="fas fa-microphone"></i> 麦克风' : |
|
|
'<i class="fas fa-microphone-slash"></i> 麦克风 (已关闭)'; |
|
|
|
|
|
toggleAudioBtn.classList.toggle('disabled', !localAudioEnabled); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function toggleVideo() { |
|
|
const videoTracks = localStream.getVideoTracks(); |
|
|
|
|
|
if (videoTracks.length > 0) { |
|
|
const track = videoTracks[0]; |
|
|
track.enabled = !track.enabled; |
|
|
localVideoEnabled = track.enabled; |
|
|
|
|
|
toggleVideoBtn.innerHTML = localVideoEnabled ? |
|
|
'<i class="fas fa-video"></i> 摄像头' : |
|
|
'<i class="fas fa-video-slash"></i> 摄像头 (已关闭)'; |
|
|
|
|
|
toggleVideoBtn.classList.toggle('disabled', !localVideoEnabled); |
|
|
|
|
|
|
|
|
const localVideoContainer = document.getElementById('local-video-container'); |
|
|
if (localVideoContainer) { |
|
|
localVideoContainer.style.backgroundColor = localVideoEnabled ? '#222' : '#444'; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async function toggleScreen() { |
|
|
if (!screenStream) { |
|
|
try { |
|
|
screenStream = await navigator.mediaDevices.getDisplayMedia({ |
|
|
video: true |
|
|
}); |
|
|
|
|
|
|
|
|
const videoTrack = screenStream.getVideoTracks()[0]; |
|
|
|
|
|
Object.values(peerConnections).forEach(pc => { |
|
|
const senders = pc.getSenders(); |
|
|
const sender = senders.find(s => s.track && s.track.kind === 'video'); |
|
|
if (sender) { |
|
|
sender.replaceTrack(videoTrack); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
const localVideo = document.querySelector('#local-video-container video'); |
|
|
if (localVideo) { |
|
|
localVideo.srcObject = screenStream; |
|
|
} |
|
|
|
|
|
toggleScreenBtn.innerHTML = '<i class="fas fa-desktop"></i> 停止共享'; |
|
|
toggleScreenBtn.classList.add('red'); |
|
|
|
|
|
|
|
|
videoTrack.onended = () => { |
|
|
stopScreenSharing(); |
|
|
}; |
|
|
} catch (error) { |
|
|
console.error('无法共享屏幕', error); |
|
|
alert('无法共享屏幕,请确保您已授予权限。'); |
|
|
} |
|
|
} else { |
|
|
stopScreenSharing(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function stopScreenSharing() { |
|
|
if (screenStream) { |
|
|
screenStream.getTracks().forEach(track => track.stop()); |
|
|
screenStream = null; |
|
|
|
|
|
|
|
|
const videoTrack = localStream.getVideoTracks()[0]; |
|
|
if (videoTrack) { |
|
|
Object.values(peerConnections).forEach(pc => { |
|
|
const senders = pc.getSenders(); |
|
|
const sender = senders.find(s => s.track && s.track.kind === 'video'); |
|
|
if (sender) { |
|
|
sender.replaceTrack(videoTrack); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
const localVideo = document.querySelector('#local-video-container video'); |
|
|
if (localVideo) { |
|
|
localVideo.srcObject = localStream; |
|
|
} |
|
|
} |
|
|
|
|
|
toggleScreenBtn.innerHTML = '<i class="fas fa-desktop"></i> 屏幕共享'; |
|
|
toggleScreenBtn.classList.remove('red'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function leaveCall() { |
|
|
|
|
|
Object.values(peerConnections).forEach(pc => pc.close()); |
|
|
peerConnections = {}; |
|
|
|
|
|
|
|
|
if (localStream) { |
|
|
localStream.getTracks().forEach(track => track.stop()); |
|
|
localStream = null; |
|
|
} |
|
|
|
|
|
|
|
|
if (screenStream) { |
|
|
screenStream.getTracks().forEach(track => track.stop()); |
|
|
screenStream = null; |
|
|
} |
|
|
|
|
|
|
|
|
if (currentRoom) { |
|
|
mockServer.disconnect(currentRoom.id, currentRoom.userId); |
|
|
currentRoom = null; |
|
|
} |
|
|
|
|
|
|
|
|
videoGrid.innerHTML = ''; |
|
|
|
|
|
|
|
|
participantsList.innerHTML = ''; |
|
|
|
|
|
|
|
|
messages.innerHTML = ''; |
|
|
|
|
|
|
|
|
meetingContainer.classList.add('hidden'); |
|
|
joinForm.classList.remove('hidden'); |
|
|
|
|
|
|
|
|
toggleAudioBtn.innerHTML = '<i class="fas fa-microphone"></i> 麦克风'; |
|
|
toggleAudioBtn.classList.remove('disabled'); |
|
|
|
|
|
toggleVideoBtn.innerHTML = '<i class="fas fa-video"></i> 摄像头'; |
|
|
toggleVideoBtn.classList.remove('disabled'); |
|
|
|
|
|
toggleScreenBtn.innerHTML = '<i class="fas fa-desktop"></i> 屏幕共享'; |
|
|
toggleScreenBtn.classList.remove('red'); |
|
|
} |
|
|
|
|
|
|
|
|
function sendMessage() { |
|
|
const messageText = messageInput.value.trim(); |
|
|
|
|
|
if (messageText && currentRoom) { |
|
|
const message = { |
|
|
text: messageText, |
|
|
sender: currentRoom.displayName, |
|
|
timestamp: new Date().toISOString() |
|
|
}; |
|
|
|
|
|
|
|
|
displayMessage(currentRoom.userId, message); |
|
|
|
|
|
|
|
|
Object.keys(dataChannels).forEach(peerId => { |
|
|
const channel = dataChannels[peerId]; |
|
|
if (channel.readyState === 'open') { |
|
|
channel.send(JSON.stringify(message)); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
messageInput.value = ''; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function displayMessage(senderId, message) { |
|
|
const messageElement = document.createElement('div'); |
|
|
messageElement.className = `message ${senderId === currentRoom?.userId ? 'sent' : 'received'}`; |
|
|
|
|
|
const senderElement = document.createElement('div'); |
|
|
senderElement.className = 'sender'; |
|
|
senderElement.textContent = message.sender; |
|
|
|
|
|
const textElement = document.createElement('div'); |
|
|
textElement.textContent = message.text; |
|
|
|
|
|
messageElement.appendChild(senderElement); |
|
|
messageElement.appendChild(textElement); |
|
|
|
|
|
messages.appendChild(messageElement); |
|
|
messages.scrollTop = messages.scrollHeight; |
|
|
} |
|
|
|
|
|
|
|
|
function copyRoomId() { |
|
|
const roomId = currentRoomIdSpan.textContent; |
|
|
navigator.clipboard.writeText(roomId) |
|
|
.then(() => { |
|
|
alert('会议 ID 已复制到剪贴板'); |
|
|
}) |
|
|
.catch(err => { |
|
|
console.error('无法复制会议 ID', err); |
|
|
alert('无法复制会议 ID,请手动选择并复制'); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
function switchTab(tabName) { |
|
|
tabs.forEach(tab => { |
|
|
tab.classList.toggle('active', tab.dataset.tab === tabName); |
|
|
}); |
|
|
|
|
|
tabContents.forEach(content => { |
|
|
content.classList.toggle('hidden', content.id !== `${tabName}Tab`); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
createMeetingBtn.addEventListener('click', () => { |
|
|
roomIdInput.value = generateId(); |
|
|
joinMeeting(); |
|
|
}); |
|
|
|
|
|
joinMeetingBtn.addEventListener('click', () => { |
|
|
joinMeeting(); |
|
|
}); |
|
|
|
|
|
toggleAudioBtn.addEventListener('click', toggleAudio); |
|
|
toggleVideoBtn.addEventListener('click', toggleVideo); |
|
|
toggleScreenBtn.addEventListener('click', toggleScreen); |
|
|
leaveCallBtn.addEventListener('click', leaveCall); |
|
|
copyRoomIdBtn.addEventListener('click', copyRoomId); |
|
|
|
|
|
tabs.forEach(tab => { |
|
|
tab.addEventListener('click', () => { |
|
|
switchTab(tab.dataset.tab); |
|
|
}); |
|
|
}); |
|
|
|
|
|
sendMessageBtn.addEventListener('click', sendMessage); |
|
|
|
|
|
messageInput.addEventListener('keypress', (event) => { |
|
|
if (event.key === 'Enter') { |
|
|
sendMessage(); |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
document.addEventListener('submit', (event) => { |
|
|
event.preventDefault(); |
|
|
}); |
|
|
</script> |
|
|
|
|
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/js/all.min.js"></script> |
|
|
</body> |
|
|
</html> |