-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
131 lines (114 loc) · 3.55 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
let socket;
let username = '';
// 获取DOM元素
const loginSection = document.querySelector('.login');
const chatSection = document.querySelector('.chat');
const usernameInput = document.getElementById('username');
const loginBtn = document.getElementById('loginBtn');
const messagesDiv = document.querySelector('.messages');
const messageInput = document.getElementById('messageInput');
const sendBtn = document.getElementById('sendBtn');
// 获取DOM元素
const loginForm = document.getElementById('loginForm');
const chatContainer = document.getElementById('chatContainer');
// 检查本地存储中是否有用户名
const storedUser = localStorage.getItem('chatUsername');
if (storedUser) {
currentUser = storedUser;
loginForm.classList.add('hidden');
chatContainer.classList.remove('hidden');
connectWebSocket();
}
// 登录处理
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
const username = usernameInput.value.trim();
if (username) {
currentUser = username;
// 保存用户名到localStorage
localStorage.setItem('chatUsername', username);
loginForm.classList.add('hidden');
chatContainer.classList.remove('hidden');
connectWebSocket();
}
});
// 登出处理
function logout() {
localStorage.removeItem('chatUsername');
location.reload();
}
// 添加登出按钮
const logoutButton = document.createElement('button');
logoutButton.textContent = '登出';
logoutButton.classList.add('logout-btn');
logoutButton.onclick = logout;
document.querySelector('.container').appendChild(logoutButton);
// 连接WebSocket服务器
function connectWebSocket() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const host = window.location.hostname === 'localhost'
? window.location.hostname
: window.location.hostname.split(':')[0];
const port = window.location.port || (protocol === 'wss:' ? 443 : 8081);
socket = new WebSocket(`${protocol}//${host}:${port}`);
socket = new WebSocket(`${protocol}//${host}:${port}`);
socket.onopen = () => {
console.log('WebSocket连接已建立');
addSystemMessage('已连接到聊天室');
};
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === 'history') {
// 显示历史消息
data.messages.forEach(message => {
displayMessage(message);
});
} else {
// 显示实时消息
displayMessage(data);
}
};
socket.onclose = () => {
addSystemMessage('连接已断开');
};
}
// 发送消息
sendBtn.addEventListener('click', sendMessage);
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
function sendMessage() {
const content = messageInput.value.trim();
if (content && socket) {
const message = {
username: currentUser,
content,
time: new Date().toLocaleTimeString()
};
socket.send(JSON.stringify(message));
messageInput.value = '';
}
}
// 显示消息
function displayMessage(message) {
const messageElement = document.createElement('div');
messageElement.classList.add('message');
messageElement.innerHTML = `
<span class="username">${message.username}</span>
<span class="time">${message.time}</span>
<div class="content">${message.content}</div>
`;
messagesDiv.appendChild(messageElement);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
}
// 显示系统消息
function addSystemMessage(content) {
const message = {
username: '系统',
content,
time: new Date().toLocaleTimeString()
};
displayMessage(message);
}