Newer
Older
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
50
import { defineStore } from 'pinia';
import { useDataStore } from '@/app/stores/data';
import type { IEntity } from '@/app/interfaces/environment';
import { useInterfaceStore } from '@/app/stores/interface';
import { useWebsocketStore } from '@/app/stores/websocket';
export const useFilesWebsocketStore = defineStore('filesWebsocketStore', () => {
const socket = ref();
const dataStore = useDataStore();
const interfaceStore = useInterfaceStore();
const homeEntities = computed(() => dataStore.homeEntities);
const filesBlob = ref([]);
onMounted(() => {
socket.value = new WebSocket('ws://localhost:5001');
socket.value.binaryType = 'arraybuffer';
socket.value.onmessage = (response: any) => {
console.log('response: ', response);
switch (response?.event) {
case 'changeHomeBackgroundUrl':
interfaceStore.setHomeBackgroundUrlFromDB(response.data?.setting_value);
break;
case 'editImageHomeEntity': {
let entities = [...dataStore.homeEntities];
entities = entities.map((entity: IEntity) => {
if (entity.entity_uuid !== response.data.entity_uuid) return entity;
return response.data;
});
dataStore.editHomeEntities(entities);
break;
}
default: {
console.log('default');
filesBlob.value.push(response);
}
}
};
socket.value.onclose = () => {
console.log('Websocket connection closed');
};
socket.value.onerror = () => {
console.log('Websocket caught an error');
};
});
function sendData(data: any) {
socket.value.send(data);
}
return { filesBlob, sendData };
});