SSE on words
This commit is contained in:
+7
-8
@@ -1,25 +1,24 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { fetchUser } from '~/service/user.service';
|
import { useUserStore } from "~/store/user.store";
|
||||||
import { useUserStore } from '~/store/user.store';
|
|
||||||
|
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const props = defineProps<Props>()
|
const props = defineProps<Props>();
|
||||||
|
|
||||||
const user = await userStore.fetchUser(props.id)
|
|
||||||
|
|
||||||
|
const user = await userStore.fetchUser(props.id);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="user">
|
<div class="user">
|
||||||
<img class="user-avatar" :src="`https://cdn.discordapp.com/avatars/${user?.id}/${user?.avatar}.webp?size=32`">
|
<img
|
||||||
|
class="user-avatar"
|
||||||
|
:src="`https://cdn.discordapp.com/avatars/${user?.id}/${user?.avatar}.webp?size=32`"
|
||||||
|
/>
|
||||||
{{ user?.username }}
|
{{ user?.username }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style lang="css">
|
<style lang="css">
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import { useWordStore } from "~/store/word.store";
|
||||||
|
import {
|
||||||
|
WordCreateEvent,
|
||||||
|
WordDeleteEvent,
|
||||||
|
WordDisableEvent,
|
||||||
|
WordEditEvent,
|
||||||
|
WordEnableEvent,
|
||||||
|
} from "../events/word.events";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export const handlers = {
|
||||||
|
create: (event: WordCreateEvent) => {
|
||||||
|
const wordStore = useWordStore();
|
||||||
|
wordStore.words[event.word.id] = event.word
|
||||||
|
},
|
||||||
|
edit: (event: WordEditEvent) => {
|
||||||
|
const wordStore = useWordStore();
|
||||||
|
wordStore.words[event.word.id].value = event.word.value
|
||||||
|
},
|
||||||
|
delete: (event: WordDeleteEvent) => {
|
||||||
|
const wordStore = useWordStore();
|
||||||
|
delete wordStore.words[event.id]
|
||||||
|
},
|
||||||
|
enable: (event: WordEnableEvent) => {
|
||||||
|
const wordStore = useWordStore();
|
||||||
|
wordStore.words[event.id].enabled = true
|
||||||
|
},
|
||||||
|
disable: (event: WordDisableEvent) => {
|
||||||
|
const wordStore = useWordStore();
|
||||||
|
wordStore.words[event.id].enabled = false
|
||||||
|
},
|
||||||
|
};
|
||||||
+20
-1
@@ -1,7 +1,26 @@
|
|||||||
<script setup lang="ts">
|
<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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
Hello
|
<div v-for="game in games" :key="game.id"></div>
|
||||||
|
<input type="button" value="Create Game" />
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
+19
-6
@@ -1,4 +1,6 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import { subscribe } from "~/netcode";
|
||||||
|
import { handlers } from "~/netcode/handlers/word.handler";
|
||||||
import { useWordStore } from "~/store/word.store";
|
import { useWordStore } from "~/store/word.store";
|
||||||
|
|
||||||
const newWord = ref("");
|
const newWord = ref("");
|
||||||
@@ -27,12 +29,23 @@ async function createWord(e: Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// watch(wordInput, (e) =>
|
let eventSource: EventSource;
|
||||||
// setInterval(() => {
|
onBeforeMount(() => {
|
||||||
// // e.focus();
|
eventSource = subscribe(`/words/subscribe`);
|
||||||
// console.log(wordInput.value);
|
eventSource.addEventListener("message", async (message) => {
|
||||||
// }, 1000)
|
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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import type { GameResponseDTO } from "~/netcode/events/dto/gameresponse.dto";
|
||||||
|
|
||||||
|
export async function getGames() {
|
||||||
|
const config = useRuntimeConfig();
|
||||||
|
const data = await useFetch<GameResponseDTO[]>(
|
||||||
|
config.public.endpoint + `/games`,
|
||||||
|
{
|
||||||
|
credentials: "include",
|
||||||
|
immediate: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return data.data
|
||||||
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
import { UserResponseDTO } from "~/netcode/events/dto/userresponse.dto";
|
|
||||||
|
|
||||||
export async function fetchUser(id: string) {
|
|
||||||
const config = useRuntimeConfig();
|
|
||||||
const data = await useFetch<UserResponseDTO>(config.public.endpoint + `/users/` + id, {
|
|
||||||
credentials: "include",
|
|
||||||
}).then(_d => {
|
|
||||||
if (_d.data.value) {
|
|
||||||
_d.data.value = new UserResponseDTO(_d.data.value)
|
|
||||||
}
|
|
||||||
return _d
|
|
||||||
})
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { defineStore } from "pinia";
|
||||||
|
import type { GameResponseDTO } from "~/netcode/events/dto/gameresponse.dto";
|
||||||
|
import { UserResponseDTO } from "~/netcode/events/dto/userresponse.dto";
|
||||||
|
|
||||||
|
export const useLobbyStore = defineStore("lobby", {
|
||||||
|
state: () => ({
|
||||||
|
games: {} as Record<string, GameResponseDTO,
|
||||||
|
}),
|
||||||
|
actions: {
|
||||||
|
async fetchGames() {
|
||||||
|
const config = useRuntimeConfig();
|
||||||
|
const data = await useFetch<GameResponseDTO[]>(config.public.endpoint + `/users/` + id, {
|
||||||
|
credentials: "include",
|
||||||
|
immediate: true
|
||||||
|
})
|
||||||
|
if(data.data.value){
|
||||||
|
for(const game of data.data.value){
|
||||||
|
this.games[game.id] = game
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data.data
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import { WordResponseDTO } from './dto/wordresponse.dto';
|
|
||||||
|
|
||||||
export class WordCreateEvent {
|
|
||||||
type: 'create';
|
|
||||||
constructor(public word: WordResponseDTO) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WordEditEvent {
|
|
||||||
type: 'edit';
|
|
||||||
constructor(public word: WordResponseDTO) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WordDeleteEvent {
|
|
||||||
type: 'delete';
|
|
||||||
constructor(public id: number) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WordEnableEvent {
|
|
||||||
type: 'enable';
|
|
||||||
constructor(public id: number) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class WordDisableEvent {
|
|
||||||
type: 'disable';
|
|
||||||
constructor(public id: number) {}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user