From 3b2a0ca231d4cd6da2ad464dc5452df56e7cc59a Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Fri, 7 Mar 2025 10:37:10 +0100 Subject: [PATCH] fix socketio reconnect --- src/App.vue | 17 ++++++++++++++- src/stores/admin/operation/connection.ts | 21 ++++--------------- src/stores/admin/operation/mission.ts | 10 +++++++++ .../admin/operation/mission/MissionList.vue | 4 +--- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/App.vue b/src/App.vue index 9be35ad..9f45d77 100644 --- a/src/App.vue +++ b/src/App.vue @@ -15,18 +15,28 @@ import { defineComponent } from "vue"; import { RouterView } from "vue-router"; import Header from "./components/Header.vue"; import Footer from "./components/Footer.vue"; -import { mapState } from "pinia"; +import { mapActions, mapState } from "pinia"; import { useAuthStore } from "./stores/auth"; import { isAuthenticatedPromise } from "./router/authGuard"; import ContextMenu from "./components/ContextMenu.vue"; import Modal from "./components/Modal.vue"; import Notification from "./components/Notification.vue"; +import { useConnectionStore } from "./stores/admin/operation/connection"; +import { useMissionStore } from "./stores/admin/operation/mission"; diff --git a/src/stores/admin/operation/connection.ts b/src/stores/admin/operation/connection.ts index cd8b636..c1f87f8 100644 --- a/src/stores/admin/operation/connection.ts +++ b/src/stores/admin/operation/connection.ts @@ -21,9 +21,8 @@ export const useConnectionStore = defineStore("connection", { const notificationStore = useNotificationStore(); this.connection?.disconnect(); this.connection = io(url, { - reconnection: false, - reconnectionDelayMax: 1000, - reconnectionAttempts: 1, + reconnection: true, + reconnectionDelay: 10000, auth: (cb) => { cb({ token: localStorage.getItem("accessToken") }); }, @@ -39,14 +38,10 @@ export const useConnectionStore = defineStore("connection", { }); this.connection.on("disconnecting", () => { this.connected = false; - this.connection?.disconnect(); }); this.connection.on("disconnect", () => { this.connected = false; - this.$reset(); - if (!this.performingManualReconnect) { - this.connectClient(); - } + this.connectClient(); }); this.connection.on("warning", (msg: string) => { notificationStore.push("Socket-Warnung", msg, "warning"); @@ -66,20 +61,12 @@ export const useConnectionStore = defineStore("connection", { if (err.message == "xhr poll error") { notificationStore.push("Socket-Netzwerk-Fehler", "Reconnect Versuch in 10s", "error"); - this.performingManualReconnect = true; - this.disconnectClient(); - setTimeout(() => { - this.connectClient(); - this.performingManualReconnect = false; - }, 10000); } else if (err.message == "Token expired") { notificationStore.push("Session", "Session wird verlängert", "info"); refreshToken() .then(() => { notificationStore.push("Session", "Session erfolgreich verlängert", "success"); - this.performingManualReconnect = true; - this.connection?.disconnect().connect(); - this.performingManualReconnect = false; + this.connection?.disconnect(); }) .catch(() => { notificationStore.push("Session-Fehler", "Anmeldung wurde nicht verlängert", "error"); diff --git a/src/stores/admin/operation/mission.ts b/src/stores/admin/operation/mission.ts index 7721659..802a39e 100644 --- a/src/stores/admin/operation/mission.ts +++ b/src/stores/admin/operation/mission.ts @@ -2,6 +2,8 @@ import { defineStore } from "pinia"; import type { MissionShortViewModel } from "@/viewmodels/admin/operation/mission.models"; import { http } from "@/serverCom"; import type { AxiosResponse } from "axios"; +import { useConnectionStore } from "./connection"; +import { useNotificationStore } from "../../notification"; export const useMissionStore = defineStore("mission", { state: () => { @@ -12,6 +14,14 @@ export const useMissionStore = defineStore("mission", { }; }, actions: { + initialize() { + const connectionStore = useConnectionStore(); + connectionStore.connection?.on("created-new-mission", (data) => { + const notificationStore = useNotificationStore(); + notificationStore.push("Neuer Einsatz", `Es wurde ein neuer Einsatz angelegt`, "info"); + this.fetchMissions(0, 25, true); + }); + }, fetchMissions(offset = 0, count = 25, clear = false) { if (clear) this.missions = []; this.loading = "loading"; diff --git a/src/views/admin/operation/mission/MissionList.vue b/src/views/admin/operation/mission/MissionList.vue index 6898c27..c6821f9 100644 --- a/src/views/admin/operation/mission/MissionList.vue +++ b/src/views/admin/operation/mission/MissionList.vue @@ -7,7 +7,7 @@ v-if="can('delete', 'operation', 'mission')" class="w-5 h-5 cursor-pointer" :class="loading == 'loading' ? 'animate-spin' : 'animate-[spin_1s_linear_1]'" - @click="fetchMissions()" + @click="fetchMissions(0, 25, true)" /> @@ -60,10 +60,8 @@ export default defineComponent({ }, mounted() { this.fetchMissions(); - this.connectClient(); }, methods: { - ...mapActions(useConnectionStore, ["connectClient"]), ...mapActions(useMissionStore, ["fetchMissions", "createMission"]), reachedEnd() { this.hasReachedEnd = true;