-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPollingChat.vue
49 lines (40 loc) · 1.28 KB
/
PollingChat.vue
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
<template>
<!-- we have no html content -->
<p />
</template>
<script setup lang="ts">
import { defineExpose, defineEmits } from "vue";
import { Message } from "@/types";
let lastMessageTimestamp = new Date().toISOString();
const emit = defineEmits(["appendMessages"]);
const sendMessage = async (msg: Message) => {
await fetch("http://localhost:8082/polling/send", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(msg),
});
};
const fetchMessages = async () => {
const response = await fetch("http://localhost:8082/polling", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: lastMessageTimestamp, // send the last message timestamp to the server
});
if (response.ok) {
const newMessages = await response.json();
emit("appendMessages", newMessages); // append the new messages to the chat
// update the last message timestamp to the timestamp of the last message, if there are any
if (newMessages.length > 0)
lastMessageTimestamp = newMessages[newMessages.length - 1].timestamp;
}
};
// we fetch new messages every 2 seconds, regardless of how long the previous request took
setInterval(fetchMessages, 2000);
defineExpose({
sendMessage,
});
</script>