-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsse-client.html
115 lines (107 loc) · 3.07 KB
/
sse-client.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>sse-demo</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
text-align: center;
padding-top: 100px;
}
input[type="text"] {
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
width: 250px;
font-size: 16px;
}
button {
padding: 10px 20px;
border-radius: 5px;
background-color: #4caf50;
color: white;
border: none;
font-size: 16px;
cursor: pointer;
}
.stop-btn {
background-color: rgb(230, 59, 125);
}
.stop-btn:hover {
background-color: rgb(177, 44, 95);
}
button:hover {
background-color: #45a049;
}
textarea {
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
width: 400px;
height: 200px;
font-size: 16px;
resize: none;
}
</style>
</head>
<body>
<input type="text" id="input" placeholder="Ask me anything..." />
<button onclick="sendRequest()">send</button>
<button class="stop-btn" onclick="closeSSE()">stop</button>
<br /><br />
<textarea
id="output"
readonly
placeholder="Welcome back! What would you like to chat about?"
></textarea>
<script>
const outputElement = document.getElementById("output");
let controller;
function sendRequest() {
const input = document.getElementById("input").value;
controller = new AbortController();
fetch("http://localhost:3001/sse", { signal: controller.signal })
.then((res) => {
if (res.status === 200) {
console.log("status 200");
return res.body;
}
})
.then((rb) => {
const reader = rb?.getReader();
let buffer = "";
reader?.read().then(function process({ done, value }) {
if (done) {
console.log("status done");
return;
}
const message = new TextDecoder("utf-8").decode(value);
buffer += message;
const lines = buffer.split("\n");
buffer = lines.pop() || "";
lines.forEach((line) => {
if (line) {
const fields = line.split(":");
const lineValue = fields[1].substr(1);
outputElement.innerHTML += JSON.parse(lineValue);
}
});
return reader?.read().then(process);
});
})
.catch((e) => {
console.log("status error");
});
}
function closeSSE() {
if (controller) {
controller.abort();
controller = undefined;
outputElement.innerHTML += `\n连接关闭\n`;
}
}
</script>
</body>
</html>