-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.vue
45 lines (39 loc) · 987 Bytes
/
app.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
<template>
<div>
<pre>
{{ messages }}
</pre>
</div>
</template>
<script setup lang="ts">
const messages = ref<unknown[]>([]);
function updateMsgs(e: MessageEvent) {
messages.value.push({
data: e.data,
origin: e.origin,
});
}
const EVENT_NAME = "update";
if (typeof window.EventSource !== "undefined") {
let source: EventSource | undefined;
onMounted(() => {
source = new window.EventSource("/api/sse");
source.addEventListener(EVENT_NAME, updateMsgs, false);
source.onerror = (err) => {
console.log("Something wrong append", err);
source?.removeEventListener(EVENT_NAME, updateMsgs);
source?.close();
source = undefined;
};
console.log("New eventSource: ", source.url);
});
onBeforeUnmount(() => {
if (source) {
console.log("Remove eventSource", source.url);
source.removeEventListener(EVENT_NAME, updateMsgs);
source.close();
source = undefined;
}
});
}
</script>