fix: case too many connection on websocket
Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
parent
a10dbbf26c
commit
c876847eff
3 changed files with 37 additions and 6 deletions
|
|
@ -9,6 +9,7 @@ import {
|
|||
recipeStreamMeta
|
||||
} from '../stores/recipeStore';
|
||||
import { buildOverviewFromServer } from '$lib/data/recipeService';
|
||||
import { auth } from '../client/firebase';
|
||||
|
||||
export const messages = writable<string[]>([]);
|
||||
|
||||
|
|
@ -118,7 +119,23 @@ const handlers: Record<string, (payload: any) => void> = {
|
|||
}
|
||||
}
|
||||
},
|
||||
stream_patch_update: (p) => {}
|
||||
stream_patch_update: (p) => {},
|
||||
notify: (p) => {
|
||||
let noti_level = p.level ?? 'INFO';
|
||||
let msg = p.msg ?? `Notify from ${p.from}`;
|
||||
let target = p.to;
|
||||
|
||||
if (target) {
|
||||
//
|
||||
let currentUsername = auth.currentUser?.displayName;
|
||||
if (currentUsername && currentUsername === target) {
|
||||
addNotification(`${noti_level}:${msg}`);
|
||||
}
|
||||
} else {
|
||||
// broadcast to all
|
||||
addNotification(`${noti_level}:${msg}`);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export function handleIncomingMessages(raw: string) {
|
||||
|
|
|
|||
|
|
@ -4,13 +4,15 @@ import { get, writable } from 'svelte/store';
|
|||
import { handleIncomingMessages } from '../handlers/messageHandler';
|
||||
import { queue as msgQueue } from '../handlers/ws_messageSender';
|
||||
|
||||
export const socketStore = writable<WebSocket | null>(null, (set) => {
|
||||
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.addEventListener('open', () => {
|
||||
set(socket);
|
||||
socketStore.set(socket);
|
||||
|
||||
// recover messages on connect, flushing
|
||||
while (get(msgQueue).length) {
|
||||
|
|
@ -29,12 +31,12 @@ export const socketStore = writable<WebSocket | null>(null, (set) => {
|
|||
});
|
||||
|
||||
socket.addEventListener('close', () => {
|
||||
set(null);
|
||||
socketStore.set(null);
|
||||
});
|
||||
|
||||
socket.addEventListener('error', (e) => {
|
||||
console.log('WebSocket error: ', e);
|
||||
set(null);
|
||||
socketStore.set(null);
|
||||
});
|
||||
|
||||
return () => {
|
||||
|
|
@ -43,4 +45,4 @@ export const socketStore = writable<WebSocket | null>(null, (set) => {
|
|||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,20 @@
|
|||
import '../layout.css';
|
||||
import ErrorLayout from '$lib/components/error-layout.svelte';
|
||||
import { sidebarStore } from '$lib/core/stores/sidebar';
|
||||
import { onMount } from 'svelte';
|
||||
import { auth } from '$lib/core/stores/auth';
|
||||
import { get } from 'svelte/store';
|
||||
import { connectToWebsocket } from '$lib/core/stores/websocketStore';
|
||||
|
||||
let { children } = $props();
|
||||
|
||||
onMount(() => {
|
||||
let currentUser = get(auth);
|
||||
console.log(`on mount layout current user: ${JSON.stringify(currentUser)}`);
|
||||
if (currentUser) {
|
||||
connectToWebsocket();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue