connect to /notify WebSocket from frontend on app startup

This commit is contained in:
j3d1 2023-11-22 21:57:14 +01:00
parent 6b3cc4c168
commit c88d434f39
2 changed files with 51 additions and 13 deletions

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
/.idea
.env
.local
staticfiles/
userfiles/

View file

@ -1,42 +1,73 @@
<template> <template>
<div id="app"> <div id="app">
<AddItemModal v-if="addModalOpen" @close="closeAddModal()" isModal="true"/> <AddItemModal v-if="addModalOpen" @close="closeAddModal()" isModal="true"/>
<Navbar @addClicked="openAddModal()"/> <Navbar @addClicked="openAddModal()"/>
<router-view/> <router-view/>
<div aria-live="polite" aria-atomic="true" class="d-flex justify-content-end align-items-start fixed-top mx-1 my-5 py-3" style="min-height: 200px; z-index: 100000; pointer-events: none"> <div aria-live="polite" aria-atomic="true"
<Toast v-for="toast in toasts" :key="toast" :title="toast.title" :message="toast.message" :color="toast.color" @close="removeToast(toast.key)" style="pointer-events: auto"/> class="d-flex justify-content-end align-items-start fixed-top mx-1 my-5 py-3"
</div> style="min-height: 200px; z-index: 100000; pointer-events: none">
<Toast v-for="toast in toasts" :key="toast" :title="toast.title" :message="toast.message" :color="toast.color"
@close="removeToast(toast.key)" style="pointer-events: auto"/>
</div> </div>
</div>
</template> </template>
<script> <script>
import Navbar from '@/components/Navbar'; import Navbar from '@/components/Navbar';
import AddItemModal from '@/components/AddItemModal'; import AddItemModal from '@/components/AddItemModal';
import Toast from './components/Toast'; import Toast from './components/Toast';
import { mapState, mapMutations } from 'vuex'; import {mapState, mapMutations} from 'vuex';
export default { export default {
name: 'app', name: 'app',
components: { Toast, Navbar, AddItemModal }, components: {Toast, Navbar, AddItemModal},
computed: mapState(['loadedItems', 'layout', 'toasts']), computed: mapState(['loadedItems', 'layout', 'toasts']),
data: () => ({ data: () => ({
addModalOpen: false addModalOpen: false
}), }),
methods: { methods: {
...mapMutations(['removeToast']), ...mapMutations(['removeToast', 'createToast']),
openAddModal() { openAddModal() {
this.addModalOpen = true; this.addModalOpen = true;
}, },
closeAddModal() { closeAddModal() {
this.addModalOpen = false; this.addModalOpen = false;
} }
},
created: function () {
this.notify_socket = new WebSocket('wss://' + window.location.host + '/notify/');
this.notify_socket.onmessage = (e) => {
let data = JSON.parse(e.data);
console.log(data, e.data);
};
this.notify_socket.onopen = (e) => {
this.createToast({
title: "Connection established",
message: JSON.stringify(e),
color: "success"
});
};
this.notify_socket.onclose = (e) => {
this.createToast({
title: "Connection closed",
message: JSON.stringify(e),
color: "danger"
});
};
this.notify_socket.onerror = (e) => {
this.createToast({
title: "Connection error",
message: JSON.stringify(e),
color: "danger"
});
};
} }
}; };
</script> </script>
<style> <style>
body, html, #app { body, html, #app {
background: #000; background: #000;
} }
</style> </style>