feat: heartbeat check if connection offline
- WIP price message Signed-off-by: pakintada@gmail.com <Pakin>
This commit is contained in:
parent
a0637c7d72
commit
4578a43197
4 changed files with 69 additions and 17 deletions
|
|
@ -60,6 +60,16 @@ async function handleAdbPayload(raw_payload: string) {
|
|||
addNotification('INFO:Machine Status Updated, ' + next);
|
||||
updateMachineStatus(next);
|
||||
}
|
||||
break;
|
||||
case 'brew-finish':
|
||||
if (payload.payload) {
|
||||
let plist = payload.payload.split('/');
|
||||
let pd = plist[0] ?? '';
|
||||
let total_time = plist[1] ?? '';
|
||||
|
||||
// update recipe data store
|
||||
}
|
||||
|
||||
break;
|
||||
case 'error':
|
||||
// show error to user from brew app
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import { buildOverviewFromServer } from '$lib/data/recipeService';
|
|||
import { auth } from '../client/firebase';
|
||||
import { type RecipeVersion } from '$lib/models/recipe_version.model';
|
||||
import { goto } from '$app/navigation';
|
||||
import { socketAlreadySendHeartbeat, socketConnectionOfflineCount } from '../stores/websocketStore';
|
||||
|
||||
export const messages = writable<string[]>([]);
|
||||
|
||||
|
|
@ -207,6 +208,18 @@ const handlers: Record<string, (payload: any) => void> = {
|
|||
|
||||
currentRecipeVersionsSelector.set(result);
|
||||
}
|
||||
},
|
||||
price: (p) => {
|
||||
let req_action = p.req_action;
|
||||
let status = p.status;
|
||||
let to = p.to;
|
||||
|
||||
let content = p.content ?? [];
|
||||
},
|
||||
heartbeat: (p) => {
|
||||
socketConnectionOfflineCount.set(0);
|
||||
socketAlreadySendHeartbeat.set(0);
|
||||
console.log('heartbeat reset offline count');
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,11 @@ import { addNotification } from './noti';
|
|||
import { permission } from './permissions';
|
||||
|
||||
let socket: WebSocket | null = null;
|
||||
let reconnectTimeout: any;
|
||||
const ENABLE_WS_DEBUG: boolean = false;
|
||||
|
||||
export const socketConnectionOfflineCount = writable<number>(0);
|
||||
export const socketAlreadySendHeartbeat = writable<number>(0);
|
||||
export const socketStore = writable<WebSocket | null>(null);
|
||||
|
||||
export function connectToWebsocket() {
|
||||
|
|
@ -28,6 +31,8 @@ export function connectToWebsocket() {
|
|||
addNotification('INFO:Connected!');
|
||||
|
||||
if (socket) {
|
||||
clearTimeout(reconnectTimeout);
|
||||
|
||||
// send auth message
|
||||
|
||||
let auth_data = get(authStore);
|
||||
|
|
@ -46,34 +51,43 @@ export function connectToWebsocket() {
|
|||
});
|
||||
}
|
||||
|
||||
// recover messages on connect, flushing
|
||||
// while (get(msgQueue).length) {
|
||||
// let queue = get(msgQueue);
|
||||
// let current = queue.shift();
|
||||
// if (current && socket) {
|
||||
// socket.send(current);
|
||||
// // set next
|
||||
// msgQueue.set(queue);
|
||||
// }
|
||||
// }
|
||||
|
||||
// heartbeat 10s
|
||||
setInterval(() => {
|
||||
if (socket) {
|
||||
if (get(socketAlreadySendHeartbeat) > 0) {
|
||||
let heartbeat_may_offline_count = get(socketConnectionOfflineCount);
|
||||
|
||||
socketConnectionOfflineCount.set(heartbeat_may_offline_count + 1);
|
||||
}
|
||||
|
||||
if (get(socketConnectionOfflineCount) > 4) {
|
||||
// counting offline 5 times then try reconnect
|
||||
socketConnectionOfflineCount.set(0);
|
||||
socketAlreadySendHeartbeat.set(0);
|
||||
|
||||
connectToWebsocket();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (socket != null) {
|
||||
sendMessage({
|
||||
type: 'heartbeat',
|
||||
payload: {}
|
||||
});
|
||||
|
||||
let heartbeat_count = get(socketAlreadySendHeartbeat);
|
||||
socketAlreadySendHeartbeat.set(heartbeat_count + 1);
|
||||
} else {
|
||||
// may send reconnect
|
||||
console.log('check on socket health', socket);
|
||||
}
|
||||
}, 10000);
|
||||
|
||||
if (auth.currentUser && socket == null) {
|
||||
console.log('try reconnect websocket ...');
|
||||
// retry again
|
||||
setTimeout(() => {
|
||||
if (socket == null) {
|
||||
connectToWebsocket();
|
||||
}
|
||||
reconnectTimeout = setTimeout(() => {
|
||||
connectToWebsocket();
|
||||
}, 5000);
|
||||
}
|
||||
});
|
||||
|
|
@ -89,7 +103,7 @@ export function connectToWebsocket() {
|
|||
if (auth.currentUser && !socket) {
|
||||
console.log('try reconnect websocket ...');
|
||||
// retry again
|
||||
setTimeout(() => connectToWebsocket(), 30000);
|
||||
reconnectTimeout = setTimeout(() => connectToWebsocket(), 5000);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -52,4 +52,19 @@ export type OutMessage =
|
|||
srv_name: string;
|
||||
values: any;
|
||||
};
|
||||
}
|
||||
| {
|
||||
type: 'price';
|
||||
payload: {
|
||||
action:
|
||||
| {
|
||||
View: string;
|
||||
}
|
||||
| {
|
||||
Edit: string;
|
||||
};
|
||||
country: string;
|
||||
parameters?: string;
|
||||
override_file?: string;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue