🐛 Websocket auto reconnecting

This commit is contained in:
LittleSheep 2024-04-02 23:31:24 +08:00
parent 9ad11f4297
commit 49bd6ea363
2 changed files with 16 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import { useRoute } from "vue-router"
export const useChannels = defineStore("channels", () => { export const useChannels = defineStore("channels", () => {
let socket: WebSocket let socket: WebSocket
let reconnectCount = 0
const done = ref(false) const done = ref(false)
@ -69,9 +70,16 @@ export const useChannels = defineStore("channels", () => {
socket.addEventListener("open", (event) => { socket.addEventListener("open", (event) => {
console.log("[MESSAGING] The unified websocket has been established... ", event.type) console.log("[MESSAGING] The unified websocket has been established... ", event.type)
reconnectCount = 0
}) })
socket.addEventListener("close", (event) => { socket.addEventListener("close", (event) => {
console.warn("[MESSAGING] The unified websocket is disconnected... ", event.reason, event.code) console.warn("[MESSAGING] The unified websocket is disconnected... ", event.reason, event.code)
if(reconnectCount <= 3) {
connect().then(() => {
console.warn("[MESSAGING] Now reconnecting!")
reconnectCount++
})
}
}) })
socket.addEventListener("message", (event) => { socket.addEventListener("message", (event) => {
const data = JSON.parse(event.data) const data = JSON.parse(event.data)

View File

@ -7,6 +7,7 @@ import { Capacitor } from "@capacitor/core"
export const useNotifications = defineStore("notifications", () => { export const useNotifications = defineStore("notifications", () => {
let socket: WebSocket let socket: WebSocket
let reconnectCount = 0
const loading = ref(false) const loading = ref(false)
@ -48,9 +49,16 @@ export const useNotifications = defineStore("notifications", () => {
socket.addEventListener("open", (event) => { socket.addEventListener("open", (event) => {
console.log("[NOTIFICATIONS] The listen websocket has been established... ", event.type) console.log("[NOTIFICATIONS] The listen websocket has been established... ", event.type)
reconnectCount = 0
}) })
socket.addEventListener("close", (event) => { socket.addEventListener("close", (event) => {
console.warn("[NOTIFICATIONS] The listen websocket is disconnected... ", event.reason, event.code) console.warn("[NOTIFICATIONS] The listen websocket is disconnected... ", event.reason, event.code)
if(reconnectCount <= 3) {
connect().then(() => {
console.warn("[NOTIFICATIONS] Now reconnecting!")
reconnectCount++
})
}
}) })
socket.addEventListener("message", (event) => { socket.addEventListener("message", (event) => {
const data = JSON.parse(event.data) const data = JSON.parse(event.data)