fix socketio reconnect
This commit is contained in:
parent
5f7062e5f1
commit
3b2a0ca231
4 changed files with 31 additions and 21 deletions
17
src/App.vue
17
src/App.vue
|
@ -15,18 +15,28 @@ import { defineComponent } from "vue";
|
||||||
import { RouterView } from "vue-router";
|
import { RouterView } from "vue-router";
|
||||||
import Header from "./components/Header.vue";
|
import Header from "./components/Header.vue";
|
||||||
import Footer from "./components/Footer.vue";
|
import Footer from "./components/Footer.vue";
|
||||||
import { mapState } from "pinia";
|
import { mapActions, mapState } from "pinia";
|
||||||
import { useAuthStore } from "./stores/auth";
|
import { useAuthStore } from "./stores/auth";
|
||||||
import { isAuthenticatedPromise } from "./router/authGuard";
|
import { isAuthenticatedPromise } from "./router/authGuard";
|
||||||
import ContextMenu from "./components/ContextMenu.vue";
|
import ContextMenu from "./components/ContextMenu.vue";
|
||||||
import Modal from "./components/Modal.vue";
|
import Modal from "./components/Modal.vue";
|
||||||
import Notification from "./components/Notification.vue";
|
import Notification from "./components/Notification.vue";
|
||||||
|
import { useConnectionStore } from "./stores/admin/operation/connection";
|
||||||
|
import { useMissionStore } from "./stores/admin/operation/mission";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
|
watch: {
|
||||||
|
connectionStatus() {
|
||||||
|
if (this.connectionStatus) {
|
||||||
|
this.initialize();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
computed: {
|
computed: {
|
||||||
...mapState(useAuthStore, ["authCheck"]),
|
...mapState(useAuthStore, ["authCheck"]),
|
||||||
|
...mapState(useConnectionStore, ["connectionStatus"]),
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
if (!this.authCheck && localStorage.getItem("access_token")) {
|
if (!this.authCheck && localStorage.getItem("access_token")) {
|
||||||
|
@ -38,11 +48,16 @@ export default defineComponent({
|
||||||
window.addEventListener("online", function (e) {
|
window.addEventListener("online", function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
|
this.connectClient();
|
||||||
},
|
},
|
||||||
beforeUnmount() {
|
beforeUnmount() {
|
||||||
window.removeEventListener("online", function (e) {
|
window.removeEventListener("online", function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
methods: {
|
||||||
|
...mapActions(useConnectionStore, ["connectClient"]),
|
||||||
|
...mapActions(useMissionStore, ["initialize"]),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -21,9 +21,8 @@ export const useConnectionStore = defineStore("connection", {
|
||||||
const notificationStore = useNotificationStore();
|
const notificationStore = useNotificationStore();
|
||||||
this.connection?.disconnect();
|
this.connection?.disconnect();
|
||||||
this.connection = io(url, {
|
this.connection = io(url, {
|
||||||
reconnection: false,
|
reconnection: true,
|
||||||
reconnectionDelayMax: 1000,
|
reconnectionDelay: 10000,
|
||||||
reconnectionAttempts: 1,
|
|
||||||
auth: (cb) => {
|
auth: (cb) => {
|
||||||
cb({ token: localStorage.getItem("accessToken") });
|
cb({ token: localStorage.getItem("accessToken") });
|
||||||
},
|
},
|
||||||
|
@ -39,14 +38,10 @@ export const useConnectionStore = defineStore("connection", {
|
||||||
});
|
});
|
||||||
this.connection.on("disconnecting", () => {
|
this.connection.on("disconnecting", () => {
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.connection?.disconnect();
|
|
||||||
});
|
});
|
||||||
this.connection.on("disconnect", () => {
|
this.connection.on("disconnect", () => {
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
this.$reset();
|
this.connectClient();
|
||||||
if (!this.performingManualReconnect) {
|
|
||||||
this.connectClient();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
this.connection.on("warning", (msg: string) => {
|
this.connection.on("warning", (msg: string) => {
|
||||||
notificationStore.push("Socket-Warnung", msg, "warning");
|
notificationStore.push("Socket-Warnung", msg, "warning");
|
||||||
|
@ -66,20 +61,12 @@ export const useConnectionStore = defineStore("connection", {
|
||||||
|
|
||||||
if (err.message == "xhr poll error") {
|
if (err.message == "xhr poll error") {
|
||||||
notificationStore.push("Socket-Netzwerk-Fehler", "Reconnect Versuch in 10s", "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") {
|
} else if (err.message == "Token expired") {
|
||||||
notificationStore.push("Session", "Session wird verlängert", "info");
|
notificationStore.push("Session", "Session wird verlängert", "info");
|
||||||
refreshToken()
|
refreshToken()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
notificationStore.push("Session", "Session erfolgreich verlängert", "success");
|
notificationStore.push("Session", "Session erfolgreich verlängert", "success");
|
||||||
this.performingManualReconnect = true;
|
this.connection?.disconnect();
|
||||||
this.connection?.disconnect().connect();
|
|
||||||
this.performingManualReconnect = false;
|
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
notificationStore.push("Session-Fehler", "Anmeldung wurde nicht verlängert", "error");
|
notificationStore.push("Session-Fehler", "Anmeldung wurde nicht verlängert", "error");
|
||||||
|
|
|
@ -2,6 +2,8 @@ import { defineStore } from "pinia";
|
||||||
import type { MissionShortViewModel } from "@/viewmodels/admin/operation/mission.models";
|
import type { MissionShortViewModel } from "@/viewmodels/admin/operation/mission.models";
|
||||||
import { http } from "@/serverCom";
|
import { http } from "@/serverCom";
|
||||||
import type { AxiosResponse } from "axios";
|
import type { AxiosResponse } from "axios";
|
||||||
|
import { useConnectionStore } from "./connection";
|
||||||
|
import { useNotificationStore } from "../../notification";
|
||||||
|
|
||||||
export const useMissionStore = defineStore("mission", {
|
export const useMissionStore = defineStore("mission", {
|
||||||
state: () => {
|
state: () => {
|
||||||
|
@ -12,6 +14,14 @@ export const useMissionStore = defineStore("mission", {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
actions: {
|
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) {
|
fetchMissions(offset = 0, count = 25, clear = false) {
|
||||||
if (clear) this.missions = [];
|
if (clear) this.missions = [];
|
||||||
this.loading = "loading";
|
this.loading = "loading";
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
v-if="can('delete', 'operation', 'mission')"
|
v-if="can('delete', 'operation', 'mission')"
|
||||||
class="w-5 h-5 cursor-pointer"
|
class="w-5 h-5 cursor-pointer"
|
||||||
:class="loading == 'loading' ? 'animate-spin' : 'animate-[spin_1s_linear_1]'"
|
:class="loading == 'loading' ? 'animate-spin' : 'animate-[spin_1s_linear_1]'"
|
||||||
@click="fetchMissions()"
|
@click="fetchMissions(0, 25, true)"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
@ -60,10 +60,8 @@ export default defineComponent({
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.fetchMissions();
|
this.fetchMissions();
|
||||||
this.connectClient();
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
...mapActions(useConnectionStore, ["connectClient"]),
|
|
||||||
...mapActions(useMissionStore, ["fetchMissions", "createMission"]),
|
...mapActions(useMissionStore, ["fetchMissions", "createMission"]),
|
||||||
reachedEnd() {
|
reachedEnd() {
|
||||||
this.hasReachedEnd = true;
|
this.hasReachedEnd = true;
|
||||||
|
|
Loading…
Add table
Reference in a new issue