-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
79 lines (71 loc) · 2.35 KB
/
Copy pathtest.html
File metadata and controls
79 lines (71 loc) · 2.35 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Send Message</title>
</head>
<body>
<h1>Send Message</h1>
<textarea
id="userInput"
placeholder="Enter usernames (e.g. @user1 @user2)"
rows="10"
></textarea>
<textarea id="messageInput" rows="4" placeholder="Enter message"></textarea>
<button id="sendButton">Send</button>
<div
id="result"
style="
white-space: pre-wrap;
border: 1px solid #ccc;
padding: 10px;
border-radius: 5px;
min-height: 300px;
width: 50%;
overflow-y: auto;
margin: 20px auto;
"
></div>
<script>
const evtSource = new EventSource("http://localhost:3000/events");
evtSource.onmessage = function (event) {
const data = JSON.parse(event.data);
console.log("Received event:", data);
// 处理接收到的事件数据
document.getElementById("result").innerHTML = data.message;
};
document
.getElementById("sendButton")
.addEventListener("click", async (event) => {
event.preventDefault(); // 阻止按钮的默认提交行为
const userInput = document.getElementById("userInput").value;
const messageInput = document.getElementById("messageInput").value;
// 检查输入是否为空
if (!userInput) {
console.log("请至少输入一个用户名。");
return;
}
try {
const response = await fetch("http://localhost:3000/launch", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ users: userInput, message: messageInput }), // 发送消息输入
});
evtSource.close();
// 仅在响应成功时记录消息
if (!response.ok) {
console.error("请求失败:", response.statusText);
return;
}
const result = await response.json();
console.log(result.message); // 将成功消息记录到控制台
} catch (error) {
console.error("发送请求时出错:", error);
}
});
</script>
</body>
</html>