forked from CrowleyRajapakse/SSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
40 lines (34 loc) · 1.2 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE Chat Application</title>
</head>
<body>
<h1>SSE Chat Application</h1>
<div id="messages"></div>
<input id="messageInput" type="text" placeholder="Type a message...">
<button onclick="sendMessage()">Send</button>
<script>
const eventSource = new EventSource('http://localhost:9095/chat/stream');
eventSource.onmessage = function(event) {
const messagesDiv = document.getElementById('messages');
const messageElement = document.createElement('div');
messageElement.innerText = event.data;
messagesDiv.appendChild(messageElement);
};
function sendMessage() {
const messageInput = document.getElementById('messageInput');
const message = messageInput.value;
fetch('http://localhost:9095/chat/send', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'message=' + encodeURIComponent(message)
});
messageInput.value = '';
}
</script>
</body>
</html>