You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

355 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<head>
<title>无痕聊天室</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background: linear-gradient(135deg, #eceff4, #f5f7fa);
transition: background 0.3s, color 0.3s;
}
body.dark {
background: #1e1e1e;
color: #ccc;
}
#chat-container {
display: flex;
flex-direction: column;
height: 100vh;
max-width: 800px;
margin: 0 auto;
background: #ffffffcc;
backdrop-filter: blur(8px);
border-radius: 12px;
overflow: hidden;
position: relative;
}
body.dark #chat-container {
background: #2c2c2ccc;
}
#chatLog {
flex: 1;
padding: 16px;
overflow-y: auto;
background: #f9f9fb;
display: flex;
flex-direction: column;
gap: 8px;
}
body.dark #chatLog {
background: #2a2a2a;
}
.message {
max-width: 75%;
padding: 10px 14px;
border-radius: 16px;
font-size: 14px;
line-height: 1.5;
word-wrap: break-word;
white-space: pre-wrap;
display: flex;
align-items: center;
justify-content: space-between;
}
.self {
align-self: flex-end;
background: linear-gradient(135deg, #409eff, #66b1ff);
color: #fff;
border-bottom-right-radius: 4px;
}
.other {
align-self: flex-start;
background: #e5e5ea;
color: #333;
border-bottom-left-radius: 4px;
}
.system {
align-self: center;
background: transparent;
color: #999;
font-size: 13px;
padding: 4px 8px;
border-radius: 8px;
}
#input-container {
padding: 12px;
background-color: #f4f6f8;
display: flex;
gap: 10px;
border-top: 1px solid #e0e0e0;
align-items: flex-end;
}
body.dark #input-container {
background-color: #333;
border-color: #444;
}
#messageInput {
flex: 1;
padding: 10px 14px;
border: 1px solid #ccc;
border-radius: 10px;
font-size: 14px;
resize: none;
min-height: 42px;
max-height: 150px;
background: #fff;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.05);
}
body.dark #messageInput {
background: #444;
color: #ccc;
border: 1px solid #666;
}
#sendButton {
padding: 10px 18px;
background: linear-gradient(135deg, #409eff, #66b1ff);
color: #fff;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
flex-shrink: 0;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
#sendButton:hover {
background: linear-gradient(135deg, #66b1ff, #85c1ff);
}
#onlineCount {
position: absolute;
top: 8px;
right: 12px;
font-size: 13px;
color: #666;
background: #eef;
padding: 4px 8px;
border-radius: 12px;
box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
body.dark #onlineCount {
background: #444;
color: #ccc;
}
#toggleDark {
position: absolute;
top: 8px;
left: 12px;
font-size: 13px;
background: #eef;
padding: 4px 8px;
border-radius: 12px;
cursor: pointer;
box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
body.dark #toggleDark {
background: #444;
color: #ccc;
}
.copy-btn {
margin-left: 8px;
background: none;
border: none;
cursor: pointer;
font-size: 13px;
color: #999;
}
.copy-btn:hover {
color: #007aff;
}
@media screen and (max-width: 600px) {
#chat-container {
max-width: 100%;
border-radius: 0;
}
}
/* 新增状态提示样式 */
#status {
position: absolute;
top: 40px;
left: 12px;
font-size: 12px;
color: #999;
}
body.dark #status {
color: #ccc;
}
</style>
</head>
<body>
<div id="chat-container">
<div id="toggleDark" onclick="toggleDarkMode()">🌙 夜间</div>
<div id="onlineCount">在线人数0</div>
<div id="status">🟢 已连接</div>
<div id="chatLog"></div>
<div id="input-container">
<textarea id="messageInput" placeholder="来说点什么吧..." rows="1"></textarea>
<button id="sendButton" onclick="sendMessage()">发送</button>
</div>
</div>
<script>
let username = localStorage.getItem("chat_username");
if (!username) {
username = prompt("请输入你的昵称:");
if (!username) {
username = "匿名用户";
}
localStorage.setItem("chat_username", username);
}
let ws;
let reconnectInterval = 2000;
let reconnectAttempts = 0;
function connectWebSocket() {
let ws_scheme = window.location.protocol === "https:" ? "wss" : "ws";
ws = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
ws.onopen = function () {
ws.send(username);
updateStatus("🟢 已连接");
document.getElementById("sendButton").disabled = false;
reconnectAttempts = 0;
};
ws.onmessage = function (event) {
const chatLog = document.getElementById("chatLog");
const msg = event.data;
if (msg.startsWith("[人数]")) {
const count = msg.replace("[人数]", "");
document.getElementById("onlineCount").textContent = "在线人数:" + count;
return;
}
const messageDiv = document.createElement("div");
const contentSpan = document.createElement("span");
let contentText = "";
if (msg.startsWith("[系统]")) {
messageDiv.className = "message system";
contentText = msg;
contentSpan.textContent = contentText;
} else {
const splitIndex = msg.indexOf("");
const sender = msg.substring(0, splitIndex);
const content = msg.substring(splitIndex + 1);
const nameSpan = document.createElement("span");
nameSpan.style.fontWeight = "bold";
nameSpan.style.marginRight = "6px";
if (sender === username) {
messageDiv.className = "message self";
nameSpan.textContent = "我:";
} else {
messageDiv.className = "message other";
nameSpan.textContent = sender + "";
}
messageDiv.appendChild(nameSpan);
contentText = content;
contentSpan.textContent = contentText;
const copyBtn = document.createElement("button");
copyBtn.textContent = "📋";
copyBtn.className = "copy-btn";
copyBtn.onclick = () => {
navigator.clipboard.writeText(contentText).then(() => {
copyBtn.textContent = "✅";
setTimeout(() => copyBtn.textContent = "📋", 1000);
});
};
messageDiv.appendChild(contentSpan);
messageDiv.appendChild(copyBtn);
}
if (msg.startsWith("[系统]")) {
messageDiv.appendChild(contentSpan);
}
chatLog.appendChild(messageDiv);
chatLog.scrollTop = chatLog.scrollHeight;
};
ws.onclose = function () {
updateStatus(`🔴 断开,尝试重连(${++reconnectAttempts})...`);
document.getElementById("sendButton").disabled = true;
setTimeout(connectWebSocket, reconnectInterval);
};
ws.onerror = function () {
ws.close();
};
}
connectWebSocket();
function sendMessage() {
const input = document.getElementById("messageInput");
if (input.value.trim() !== "" && ws.readyState === WebSocket.OPEN) {
ws.send(input.value.trim());
input.value = "";
adjustInputHeight();
}
}
document.getElementById("messageInput").addEventListener("keydown", function (event) {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
sendMessage();
}
});
document.getElementById("messageInput").addEventListener("input", adjustInputHeight);
function adjustInputHeight() {
const input = document.getElementById("messageInput");
input.style.height = "auto";
input.style.height = (input.scrollHeight) + "px";
}
function toggleDarkMode() {
document.body.classList.toggle("dark");
const btn = document.getElementById("toggleDark");
if (document.body.classList.contains("dark")) {
btn.textContent = "☀️ 日间";
} else {
btn.textContent = "🌙 夜间";
}
}
function updateStatus(text) {
document.getElementById("status").textContent = text;
}
</script>
</body>
</html>