|
|
<!DOCTYPE html>
|
|
|
<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);
|
|
|
}
|
|
|
|
|
|
#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;
|
|
|
}
|
|
|
|
|
|
#chatLog {
|
|
|
flex: 1;
|
|
|
padding: 16px;
|
|
|
overflow-y: auto;
|
|
|
background: #f9f9fb;
|
|
|
display: flex;
|
|
|
flex-direction: column;
|
|
|
gap: 8px;
|
|
|
}
|
|
|
|
|
|
.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;
|
|
|
}
|
|
|
|
|
|
.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;
|
|
|
}
|
|
|
|
|
|
#input-container {
|
|
|
padding: 12px;
|
|
|
background-color: #f4f6f8;
|
|
|
display: flex;
|
|
|
gap: 10px;
|
|
|
border-top: 1px solid #e0e0e0;
|
|
|
align-items: flex-end;
|
|
|
}
|
|
|
|
|
|
#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);
|
|
|
}
|
|
|
|
|
|
#messageInput:focus {
|
|
|
border-color: #999;
|
|
|
outline: none;
|
|
|
}
|
|
|
|
|
|
#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);
|
|
|
}
|
|
|
|
|
|
/* 手机适配 */
|
|
|
@media screen and (max-width: 600px) {
|
|
|
#chat-container {
|
|
|
max-width: 100%;
|
|
|
border-radius: 0;
|
|
|
}
|
|
|
|
|
|
.message {
|
|
|
font-size: 15px;
|
|
|
padding: 10px 14px;
|
|
|
}
|
|
|
|
|
|
#messageInput {
|
|
|
font-size: 16px;
|
|
|
padding: 10px 14px;
|
|
|
}
|
|
|
|
|
|
#sendButton {
|
|
|
padding: 10px 14px;
|
|
|
font-size: 15px;
|
|
|
}
|
|
|
|
|
|
#input-container {
|
|
|
padding: 10px;
|
|
|
gap: 8px;
|
|
|
}
|
|
|
}
|
|
|
</style>
|
|
|
</head>
|
|
|
<body>
|
|
|
<div id="chat-container">
|
|
|
<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_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");
|
|
|
const messageDiv = document.createElement("div");
|
|
|
|
|
|
// 判断是否是自己发的
|
|
|
if (event.data.startsWith(username + ":")) {
|
|
|
messageDiv.className = "message self";
|
|
|
messageDiv.textContent = event.data.replace(username + ":", "我:");
|
|
|
} else {
|
|
|
messageDiv.className = "message other";
|
|
|
messageDiv.textContent = event.data;
|
|
|
}
|
|
|
|
|
|
chatLog.appendChild(messageDiv);
|
|
|
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> |