SSE on words

This commit is contained in:
2024-12-29 22:06:57 +01:00
parent 29ffc556ec
commit e0ece631d3
8 changed files with 126 additions and 65 deletions
+20 -1
View File
@@ -1,7 +1,26 @@
<script setup lang="ts">
import { subscribe } from "~/netcode";
import { getGames } from "~/service/game.service";
let eventSource: EventSource;
const games = await getGames();
onBeforeMount(() => {
eventSource = subscribe(`/games/subscribe`);
eventSource.addEventListener("message", async (message) => {
console.log(message);
});
});
onUnmounted(() => {
if (eventSource) {
eventSource.close();
}
});
</script>
<template>
Hello
<div v-for="game in games" :key="game.id"></div>
<input type="button" value="Create Game" />
</template>
+19 -6
View File
@@ -1,4 +1,6 @@
<script setup lang="ts">
import { subscribe } from "~/netcode";
import { handlers } from "~/netcode/handlers/word.handler";
import { useWordStore } from "~/store/word.store";
const newWord = ref("");
@@ -27,12 +29,23 @@ async function createWord(e: Event) {
}
}
// watch(wordInput, (e) =>
// setInterval(() => {
// // e.focus();
// console.log(wordInput.value);
// }, 1000)
// );
let eventSource: EventSource;
onBeforeMount(() => {
eventSource = subscribe(`/words/subscribe`);
eventSource.addEventListener("message", async (message) => {
const data = JSON.parse(message.data);
const type = data.type as string;
if (type in handlers) {
handlers[type as keyof typeof handlers](data);
}
});
});
onUnmounted(() => {
if (eventSource) {
eventSource.close();
}
});
</script>
<template>