腾讯体育为您带来现场的 NBA 篮球盛宴
【发布时间】:2024-05-06 14:50:28
<link href="style.css" rel="stylesheet"/>
<script>
// 实时更新数据const score = document.getElementById("score");const rebounds = document.getElementById("rebounds");const assists = document.getElementById("assists");const steals = document.getElementById("steals");const blocks = document.getElementById("blocks");const team1Record = document.getElementById("team-1-record");const team2Record = document.getElementById("team-2-record");(function () {const socket = new WebSocket("ws://" + window.location.host + "/ws");socket.onmessage = function (event) {const data = JSON.parse(event.data);switch (data.type) {case "stats":score.innerHTML = data.score;rebounds.innerHTML = data.rebounds;assists.innerHTML = data.assists;steals.innerHTML = data.steals;blocks.innerHTML = data.blocks;break;case "team-info":team1Record.innerHTML = data.team1Record;team2Record.innerHTML = data.team2Record;break;}};})();// 聊天功能const chatForm = document.getElementById("chat-form");const chatInput = document.getElementById("chat-input");const chatMessages = document.getElementById("chat-messages");chatForm.addEventListener("submit", function (event) {event.preventDefault();const message = chatInput.value;if (message === "") {return;}const data = {type: "chat",message: message,};const socket = new WebSocket("ws://" + window.location.host + "/ws");socket.onopen = function () {socket.send(JSON.stringify(data));};socket.onclose = function () {chatMessages.innerHTML += "
无法连接到聊天服务器";};socket.onmessage = function (event) {const data = JSON.parse(event.data);switch (data.type) {case "chat":chatMessages.innerHTML += `
${data.message}`;break;}};chatInput.value = "";});
</script>