forked from billmian/socketio-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
155 lines (141 loc) · 4.79 KB
/
index.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>聊天室</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="body-container">
<div class="live-container">
<div class="live-title-container">
<div class="live-title">在线人数:</div>
<div id="live-number"></div>
</div>
<div id="live-people"></div>
</div>
<div class="content-container">
<div class="content">
<div id="messages"></div>
<div class="user-message">
<textarea id="message-textArea" autocomplete="off"></textarea>
<button id="message-button" class="send-button">发送</button>
</div>
</div>
</div>
</div>
</body>
<script src="/socket.io/socket.io.js"></script>
<script>
const cookie = document.cookie;
cookieObj = cookie.split(";").reduce((all, item, index) => {
keyAndValue = item.trim().split("=");
all[keyAndValue[0]] = keyAndValue[1];
return all;
}, {});
const name = cookieObj["userName"];
console.log("🚀 ~ file: index.html ~ line 37 ~ name", name)
if (!name) {
window.location.href = "http://localhost:3000/login";
}
var socket = io({ query: { userName: name } });
const messageList = [];
const messageContent = document.querySelector("#messages");
const messageButton = document.querySelector("#message-button");
const messageTextArea = document.querySelector("#message-textArea");
const livePeople = document.querySelector("#live-people");
const livePeopleNumber = document.querySelector("#live-number");
messageButton.addEventListener("click", () => {
const sendTime = new Date().valueOf();
const sendMessageObj = {
name,
message: messageTextArea.value,
time: sendTime,
};
socket.emit("newChatMessage", sendMessageObj);
messageTextArea.value = "";
});
socket.on("broadCastMessage", (msg) => {
messageList.push(msg);
renderContent(msg);
});
//有其他人进入了聊天室
socket.on("connectUser", (userList) => {
renderLivePeople(userList);
livePeopleNumber.innerText = userList.length;
});
//有人退出了聊天室
socket.on("disconUser", (userList) => {
renderLivePeople(userList);
livePeopleNumber.innerText = userList.length;
});
function formatDate(date) {
if (date < 10) {
return "0" + date;
} else {
return date;
}
}
function renderLivePeople(list) {
//清除所有子节点
while (livePeople.hasChildNodes()) {
livePeople.removeChild(livePeople.firstChild);
}
list.forEach((element) => {
let livePersonNode = document.createElement("div");
livePersonNode.className = "live-person";
livePersonNode.innerText = element;
livePeople.appendChild(livePersonNode);
});
}
function renderContent(msg) {
let newNode;
if (msg.name === name) {
newContainer = document.createElement("div");
newContainer.className = "my-message-container";
userInfo = document.createElement("div");
userInfo.className = "my-message-info";
userInfo.innerText =
msg.name +
" " +
new Date(msg.time).getFullYear() +
"/" +
formatDate(new Date(msg.time).getMonth() + 1) +
"/" +
formatDate(new Date(msg.time).getDate()) +
" " +
formatDate(new Date(msg.time).getHours()) +
":" +
formatDate(new Date(msg.time).getMinutes());
newNode = document.createElement("div");
newNode.className = "my-message-content";
newNode.innerText = msg.message;
newContainer.appendChild(userInfo);
newContainer.appendChild(newNode);
} else {
newContainer = document.createElement("div");
newContainer.className = "custom-message-container";
userInfo = document.createElement("div");
userInfo.className = "custom-message-info";
userInfo.innerText =
msg.name +
" " +
new Date(msg.time).getFullYear() +
"/" +
formatDate(new Date(msg.time).getMonth() + 1) +
"/" +
formatDate(new Date(msg.time).getDate()) +
" " +
formatDate(new Date(msg.time).getHours()) +
":" +
formatDate(new Date(msg.time).getMinutes());
newNode = document.createElement("div");
newNode.className = "custom-message-content";
newNode.innerText = msg.message;
newContainer.appendChild(userInfo);
newContainer.appendChild(newNode);
}
messageContent.appendChild(newContainer);
}
</script>
</html>