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);
|
addNotification('INFO:Machine Status Updated, ' + next);
|
||||||
updateMachineStatus(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;
|
break;
|
||||||
case 'error':
|
case 'error':
|
||||||
// show error to user from brew app
|
// show error to user from brew app
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import { buildOverviewFromServer } from '$lib/data/recipeService';
|
||||||
import { auth } from '../client/firebase';
|
import { auth } from '../client/firebase';
|
||||||
import { type RecipeVersion } from '$lib/models/recipe_version.model';
|
import { type RecipeVersion } from '$lib/models/recipe_version.model';
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
|
import { socketAlreadySendHeartbeat, socketConnectionOfflineCount } from '../stores/websocketStore';
|
||||||
|
|
||||||
export const messages = writable<string[]>([]);
|
export const messages = writable<string[]>([]);
|
||||||
|
|
||||||
|
|
@ -207,6 +208,18 @@ const handlers: Record<string, (payload: any) => void> = {
|
||||||
|
|
||||||
currentRecipeVersionsSelector.set(result);
|
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';
|
import { permission } from './permissions';
|
||||||
|
|
||||||
let socket: WebSocket | null = null;
|
let socket: WebSocket | null = null;
|
||||||
|
let reconnectTimeout: any;
|
||||||
const ENABLE_WS_DEBUG: boolean = false;
|
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 const socketStore = writable<WebSocket | null>(null);
|
||||||
|
|
||||||
export function connectToWebsocket() {
|
export function connectToWebsocket() {
|
||||||
|
|
@ -28,6 +31,8 @@ export function connectToWebsocket() {
|
||||||
addNotification('INFO:Connected!');
|
addNotification('INFO:Connected!');
|
||||||
|
|
||||||
if (socket) {
|
if (socket) {
|
||||||
|
clearTimeout(reconnectTimeout);
|
||||||
|
|
||||||
// send auth message
|
// send auth message
|
||||||
|
|
||||||
let auth_data = get(authStore);
|
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
|
// heartbeat 10s
|
||||||
setInterval(() => {
|
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({
|
sendMessage({
|
||||||
type: 'heartbeat',
|
type: 'heartbeat',
|
||||||
payload: {}
|
payload: {}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let heartbeat_count = get(socketAlreadySendHeartbeat);
|
||||||
|
socketAlreadySendHeartbeat.set(heartbeat_count + 1);
|
||||||
|
} else {
|
||||||
|
// may send reconnect
|
||||||
|
console.log('check on socket health', socket);
|
||||||
}
|
}
|
||||||
}, 10000);
|
}, 10000);
|
||||||
|
|
||||||
if (auth.currentUser && socket == null) {
|
if (auth.currentUser && socket == null) {
|
||||||
console.log('try reconnect websocket ...');
|
console.log('try reconnect websocket ...');
|
||||||
// retry again
|
// retry again
|
||||||
setTimeout(() => {
|
reconnectTimeout = setTimeout(() => {
|
||||||
if (socket == null) {
|
connectToWebsocket();
|
||||||
connectToWebsocket();
|
|
||||||
}
|
|
||||||
}, 5000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -89,7 +103,7 @@ export function connectToWebsocket() {
|
||||||
if (auth.currentUser && !socket) {
|
if (auth.currentUser && !socket) {
|
||||||
console.log('try reconnect websocket ...');
|
console.log('try reconnect websocket ...');
|
||||||
// retry again
|
// retry again
|
||||||
setTimeout(() => connectToWebsocket(), 30000);
|
reconnectTimeout = setTimeout(() => connectToWebsocket(), 5000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,4 +52,19 @@ export type OutMessage =
|
||||||
srv_name: string;
|
srv_name: string;
|
||||||
values: any;
|
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