feat: add retry connect socket, close when logged out
Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
parent
c876847eff
commit
4866674f26
2 changed files with 21 additions and 3 deletions
|
|
@ -14,6 +14,7 @@
|
|||
import * as adb from '$lib/core/adb/adb';
|
||||
import { browser } from '$app/environment';
|
||||
import { deleteCookiesOnNonBrowser } from '$lib/helpers/cookie';
|
||||
import { socketStore } from '$lib/core/stores/websocketStore';
|
||||
|
||||
const sidebar = useSidebar();
|
||||
|
||||
|
|
@ -44,6 +45,13 @@
|
|||
}
|
||||
|
||||
authStore.set(null);
|
||||
|
||||
let socket = get(socketStore);
|
||||
if (socket) {
|
||||
socket.close(1011, 'logout');
|
||||
}
|
||||
|
||||
socketStore.set(null);
|
||||
if (browser && 'cookieStore' in window) await cookieStore.delete('logged_in');
|
||||
else deleteCookiesOnNonBrowser('logged_in');
|
||||
await auth.signOut();
|
||||
|
|
|
|||
|
|
@ -3,13 +3,16 @@ import { env } from '$env/dynamic/public';
|
|||
import { get, writable } from 'svelte/store';
|
||||
import { handleIncomingMessages } from '../handlers/messageHandler';
|
||||
import { queue as msgQueue } from '../handlers/ws_messageSender';
|
||||
import { auth } from '../client/firebase';
|
||||
|
||||
let socket: WebSocket | null = null;
|
||||
|
||||
export const socketStore = writable<WebSocket | null>(null);
|
||||
|
||||
export function connectToWebsocket() {
|
||||
if (browser) {
|
||||
console.log('connecting to ', env.PUBLIC_WSS);
|
||||
const socket = new WebSocket(`${env.PUBLIC_WSS}`);
|
||||
socket = new WebSocket(`${env.PUBLIC_WSS}`);
|
||||
|
||||
socket.addEventListener('open', () => {
|
||||
socketStore.set(socket);
|
||||
|
|
@ -18,7 +21,7 @@ export function connectToWebsocket() {
|
|||
while (get(msgQueue).length) {
|
||||
let queue = get(msgQueue);
|
||||
let current = queue.shift();
|
||||
if (current) {
|
||||
if (current && socket) {
|
||||
socket.send(current);
|
||||
// set next
|
||||
msgQueue.set(queue);
|
||||
|
|
@ -32,6 +35,13 @@ export function connectToWebsocket() {
|
|||
|
||||
socket.addEventListener('close', () => {
|
||||
socketStore.set(null);
|
||||
socket = null;
|
||||
|
||||
if (auth.currentUser) {
|
||||
console.log('try reconnect websocket ...');
|
||||
// retry again
|
||||
setTimeout(() => connectToWebsocket(), 5000);
|
||||
}
|
||||
});
|
||||
|
||||
socket.addEventListener('error', (e) => {
|
||||
|
|
@ -40,7 +50,7 @@ export function connectToWebsocket() {
|
|||
});
|
||||
|
||||
return () => {
|
||||
if (socket.readyState === WebSocket.OPEN) {
|
||||
if (socket?.readyState === WebSocket.OPEN) {
|
||||
socket.close();
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue