sync to server

This commit is contained in:
Julian Krauser 2025-02-26 16:26:43 +01:00
parent 35a3c2f666
commit d1cb00a4d0
5 changed files with 46 additions and 84 deletions

View file

@ -5,12 +5,16 @@ import App from "./App.vue";
import router from "./router";
import NProgress from "nprogress";
import "../node_modules/nprogress/nprogress.css";
import { Quill } from "@vueup/vue-quill";
import QuillCursors from "quill-cursors";
import { http } from "./serverCom";
import "./main.css";
NProgress.configure({ showSpinner: false });
Quill.register("modules/cursors", QuillCursors);
const app = createApp(App);
app.use(createPinia());

View file

@ -1,5 +1,5 @@
import { defineStore } from "pinia";
import { io, type Socket } from "socket.io-client";
import { io, Socket } from "socket.io-client";
import { useNotificationStore } from "../../notification";
import { refreshToken, url } from "../../../serverCom";
@ -19,7 +19,8 @@ export const useConnectionStore = defineStore("connection", {
this.connection?.disconnect();
this.connection = io(url, {
reconnection: true,
reconnectionDelayMax: 10000,
reconnectionDelayMax: 1000,
reconnectionAttempts: 1,
auth: (cb) => {
cb({ token: localStorage.getItem("accessToken") });
},
@ -46,18 +47,6 @@ export const useConnectionStore = defineStore("connection", {
message: msg,
});
});
this.connection.on("reconnect", (attemptNumber) => {
notificationStore.push("Socket-Reconnect", `Reconnect erfolgreich`, "success");
});
this.connection.on("reconnect_attempt", (attemptNumber) => {
notificationStore.push("Socket-Reconnect-Versuch", `Reconnect-Versuch Nr.${attemptNumber}`, "info");
});
this.connection.on("reconnect_error", (error) => {
notificationStore.push("Socket-Warnung", "Reconnect fehlgeschlagen", "warning");
});
this.connection.on("reconnect_failed", () => {
notificationStore.push("Socket-Fehler", "Reconnect-Versuche erfolglos beendet", "error");
});
},
disconnectClient(): void {
this.connection?.disconnect();
@ -67,6 +56,11 @@ export const useConnectionStore = defineStore("connection", {
if (err.message == "xhr poll error") {
notificationStore.push("Socket-Netzwerk-Fehler", "Reconnect Versuch in 10s", "error");
this.disconnectClient();
setTimeout(() => {
console.log("hi");
this.connectClient();
}, 10000);
} else if (err.message == "Token expired") {
notificationStore.push("Session", "Session wird verlängert", "info");
refreshToken()

View file

@ -4,9 +4,7 @@ import * as Y from "yjs";
import { Awareness } from "y-protocols/awareness.js";
import { computed, ref } from "vue";
export const useMissionDetailStore = defineStore(
"missionDetail",
() => {
export const useMissionDetailStore = defineStore("missionDetail", () => {
const connectionStore = useConnectionStore();
const ydoc = ref(new Y.Doc());
@ -24,59 +22,28 @@ export const useMissionDetailStore = defineStore(
});
function init(missionId: string) {
connectionStore.connection?.emit("room:join", missionId);
connectionStore.connection?.on("sync", (update) => {
Y.applyUpdate(ydoc.value, update);
connectionStore.connection?.on("package-sync", (update) => {
Y.applyUpdate(ydoc.value, new Uint8Array(update));
});
connectionStore.connection?.on("package-mission", (initial) => {
Y.applyUpdate(ydoc.value, new Uint8Array(initial));
});
connectionStore.connection?.emit("mission:join", missionId);
}
function destroy() {
connectionStore?.connection?.emit("room:leave");
useMissionDetailStore().$reset();
}
ydoc.value.on("update", (update) => {
console.log(Array.from(update));
connectionStore.connection?.emit("sync", Array.from(update));
connectionStore?.connection?.emit("mission:leave");
ydoc.value = new Y.Doc();
awareness.value = new Awareness(ydoc.value);
editor.value = ydoc.value.getText("editor");
}
ydoc.value.on("update", (update) => {
console.log(connectionStore.connection);
connectionStore.connection?.emit("mission:sync", Array.from(update));
});
return { ydoc, awareness, editor, title, init, destroy };
}
// {
// state: () => {
// const ydoc = new Y.Doc();
// return {
// ydoc: ydoc as Y.Doc,
// awareness: new Awareness(ydoc) as Awareness,
// editor: "",
// };
// },
// actions: {
// init(missionId: string) {
// const connectionStore = useConnectionStore();
// if (!connectionStore.connection) return;
// connectionStore.connection.emit("room:join", missionId);
// connectionStore.connection.on("sync", (update) => {
// Y.applyUpdate(this.ydoc, update);
// });
// this.$subscribe((mutation, state) => {
// console.log(mutation, state);
// });
// },
// destroy() {
// const connectionStore = useConnectionStore();
// if (!connectionStore.connection) return;
// connectionStore.connection.emit("room:leave");
// this.$reset();
// },
// },
// }
);
});

View file

@ -13,7 +13,6 @@
<label for="title">Einsatzbeginn</label>
<input type="datetime-local" id="title" />
</div>
{{ title }}
<div class="grow">
<label for="title">Einsatzende</label>
<input type="datetime-local" id="title" />
@ -83,7 +82,7 @@ import { defineComponent } from "vue";
import { mapActions, mapState, mapWritableState } from "pinia";
import { useAbilityStore } from "@/stores/ability";
import { Quill, QuillEditor } from "@vueup/vue-quill";
import QuillCursors from "quill-cursors";
import type QuillCursors from "quill-cursors";
import "@vueup/vue-quill/dist/vue-quill.snow.css";
import { QuillBinding } from "y-quill";
import { moduleOptions } from "@/helpers/quillConfig";
@ -105,9 +104,6 @@ export default defineComponent({
...mapState(useForceStore, ["availableForces"]),
...mapWritableState(useMissionDetailStore, ["editor", "title"]),
},
created() {
Quill.register("modules/cursors", QuillCursors);
},
methods: {
initEditor(quill: Quill) {
this.binding = new QuillBinding(this.editor, quill);

View file

@ -69,6 +69,7 @@ export default defineComponent({
},
connectionStatus() {
if (this.connectionStatus) {
console.log("hi");
this.init(this.id);
}
},