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.

140 lines
4.1 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>
<html>
<head>
<title>纵有千古、横有八荒</title>
<meta charset="utf-8">
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background: linear-gradient(135deg, #eceff4, #f5f7fa);
}
#chat-container {
display: flex;
flex-direction: column;
height: 100vh;
max-width: 800px;
margin: 0 auto;
box-shadow: 0 0 10px rgba(0,0,0,0.05);
background: #ffffffcc;
backdrop-filter: blur(8px);
border-radius: 12px;
overflow: hidden;
}
#chatLog {
flex: 1;
padding: 16px;
border: none;
resize: none;
background: #f9f9fb;
overflow-y: auto;
font-size: 14px;
line-height: 1.6;
color: #333;
border-bottom: 1px solid #e0e0e0;
}
#chatLog:focus {
outline: none;
}
#input-container {
padding: 14px;
background-color: #f4f6f8;
display: flex;
gap: 10px;
border-top: 1px solid #e0e0e0;
}
#messageInput {
flex: 1;
padding: 10px 14px;
border: 1px solid #ccc;
border-radius: 10px;
font-size: 14px;
resize: none;
min-height: 42px;
background: #fff;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.05);
}
#messageInput:focus {
border-color: #999;
outline: none;
}
#sendButton {
padding: 0 20px;
background: linear-gradient(135deg, #409eff, #66b1ff);
color: #fff;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
#sendButton:hover {
background: linear-gradient(135deg, #66b1ff, #85c1ff);
}
</style>
</head>
<body>
<div id="chat-container">
<textarea id="chatLog" readonly></textarea>
<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_scheme = window.location.protocol === "https:" ? "wss" : "ws";
let ws = new WebSocket(ws_scheme + "://" + window.location.host + "/ws");
ws.onmessage = function(event) {
const chatLog = document.getElementById("chatLog");
chatLog.value += event.data + "\n";
chatLog.scrollTop = chatLog.scrollHeight;
};
function sendMessage() {
const input = document.getElementById("messageInput");
if (input.value.trim() !== "") {
const message = username + "" + input.value.trim();
ws.send(message);
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";
}
</script>
</body>
</html>