<script>
// 获取直播流地址const liveStreamUrl = 'https://live.domain.com/stream.m3u8';// 创建视频元素const video = document.getElementById('live-stream');video.src = liveStreamUrl;video.autoplay = true;// 创建聊天框元素const chatMessages = document.getElementById('chat-messages');const chatInput = document.getElementById('chat-input');// 创建 WebSocket 连接const websocket = new WebSocket('wss://chat.domain.com/ws');// WebSocket 打开时websocket.onopen = () => {console.log('WebSocket 已连接');};// WebSocket 收到消息时websocket.onmessage = (event) => {// 解析收到的消息const data = JSON.parse(event.data);// 显示消息chatMessages.innerHTML += `
${data.user}: ${data.message}
`;chatMessages.scrollTop = chatMessages.scrollHeight;};// WebSocket 关闭时websocket.onclose = () => {console.log('WebSocket 已关闭');};// 发送消息chatInput.addEventListener('keypress', (event) => {if (event.key === 'Enter') {// 获取输入的消息const message = chatInput.value;// 发送消息到 WebSocketwebsocket.send(JSON.stringify({ message: message }));// 清空输入框chatInput.value = '';}});
</script>