new: [app] Added keepalive messages
This commit is contained in:
parent
34869497a1
commit
e16fc0c7cd
3 changed files with 45 additions and 11 deletions
15
server.py
15
server.py
|
@ -18,6 +18,7 @@ import misp_api
|
||||||
|
|
||||||
|
|
||||||
ZMQ_MESSAGE_COUNT = 0
|
ZMQ_MESSAGE_COUNT = 0
|
||||||
|
ZMQ_LAST_TIME = None
|
||||||
|
|
||||||
|
|
||||||
def debounce(debounce_seconds: int = 1):
|
def debounce(debounce_seconds: int = 1):
|
||||||
|
@ -182,15 +183,26 @@ async def getDiagnostic() -> dict:
|
||||||
return diagnostic
|
return diagnostic
|
||||||
|
|
||||||
|
|
||||||
|
async def keepalive():
|
||||||
|
global ZMQ_LAST_TIME
|
||||||
|
while True:
|
||||||
|
await sio.sleep(5)
|
||||||
|
payload = {
|
||||||
|
'zmq_last_time': ZMQ_LAST_TIME,
|
||||||
|
}
|
||||||
|
await sio.emit('keep_alive', payload)
|
||||||
|
|
||||||
|
|
||||||
# Function to forward zmq messages to Socket.IO
|
# Function to forward zmq messages to Socket.IO
|
||||||
async def forward_zmq_to_socketio():
|
async def forward_zmq_to_socketio():
|
||||||
global ZMQ_MESSAGE_COUNT
|
global ZMQ_MESSAGE_COUNT, ZMQ_LAST_TIME
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
message = await zsocket.recv_string()
|
message = await zsocket.recv_string()
|
||||||
topic, s, m = message.partition(" ")
|
topic, s, m = message.partition(" ")
|
||||||
try:
|
try:
|
||||||
ZMQ_MESSAGE_COUNT += 1
|
ZMQ_MESSAGE_COUNT += 1
|
||||||
|
ZMQ_LAST_TIME = time.time()
|
||||||
await handleMessage(topic, s, m)
|
await handleMessage(topic, s, m)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error('Error handling message %s', e)
|
logger.error('Error handling message %s', e)
|
||||||
|
@ -198,6 +210,7 @@ async def forward_zmq_to_socketio():
|
||||||
|
|
||||||
async def init_app():
|
async def init_app():
|
||||||
sio.start_background_task(forward_zmq_to_socketio)
|
sio.start_background_task(forward_zmq_to_socketio)
|
||||||
|
sio.start_background_task(keepalive)
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,31 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { socketConnected } from "@/socket";
|
import { computed } from "vue"
|
||||||
|
import { socketConnected, zmqLastTime } from "@/socket";
|
||||||
|
|
||||||
|
const zmqLastTimeSecond = computed(() => {
|
||||||
|
return parseInt(((new Date()).getTime() - zmqLastTime.value * 1000) / 1000)
|
||||||
|
})
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<span :class="{
|
<span class="flex flex-col justify-center mt-1">
|
||||||
'px-2 py-1 rounded-md inline-block w-48': true,
|
<span :class="{
|
||||||
'text-slate-900 dark:text-slate-400': socketConnected,
|
'px-2 rounded-md inline-block w-48 leading-4': true,
|
||||||
'text-slate-50 bg-red-600': !socketConnected,
|
'text-slate-900 dark:text-slate-400': socketConnected,
|
||||||
}">
|
'text-slate-50 bg-red-600': !socketConnected,
|
||||||
<span class="mr-1">Socket.IO:</span>
|
}">
|
||||||
<span v-show="socketConnected" class="font-semibold text-green-600 dark:text-green-400">Connected</span>
|
<span class="mr-1">Socket.IO:</span>
|
||||||
<span v-show="!socketConnected" class="font-semibold text-slate-50">Disconnected</span>
|
<span v-show="socketConnected" class="font-semibold text-green-600 dark:text-green-400">Connected</span>
|
||||||
|
<span v-show="!socketConnected" class="font-semibold text-slate-50">Disconnected</span>
|
||||||
|
</span>
|
||||||
|
<span class="text-xs font-thin leading-3 inline-block text-center">
|
||||||
|
<template v-if="zmqLastTimeSecond == 0">
|
||||||
|
online
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
{{ zmqLastTimeSecond }}s
|
||||||
|
</template>
|
||||||
|
</span>
|
||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
|
@ -18,7 +18,8 @@ const initial_state = {
|
||||||
|
|
||||||
const state = reactive({ ...initial_state });
|
const state = reactive({ ...initial_state });
|
||||||
const connectionState = reactive({
|
const connectionState = reactive({
|
||||||
connected: false
|
connected: false,
|
||||||
|
zmq_last_time: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,6 +41,7 @@ export const notificationAPICounter = computed(() => state.notificationAPICounte
|
||||||
export const userCount = computed(() => Object.keys(state.progresses).length)
|
export const userCount = computed(() => Object.keys(state.progresses).length)
|
||||||
export const diagnostic = computed(() => state.diagnostic)
|
export const diagnostic = computed(() => state.diagnostic)
|
||||||
export const socketConnected = computed(() => connectionState.connected)
|
export const socketConnected = computed(() => connectionState.connected)
|
||||||
|
export const zmqLastTime = computed(() => connectionState.zmq_last_time)
|
||||||
|
|
||||||
export function resetState() {
|
export function resetState() {
|
||||||
Object.assign(state, initial_state);
|
Object.assign(state, initial_state);
|
||||||
|
@ -188,6 +190,10 @@ socket.on("refresh_score", (new_user) => {
|
||||||
debouncedGetProgress()
|
debouncedGetProgress()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on("keep_alive", (keep_alive) => {
|
||||||
|
connectionState.zmq_last_time = keep_alive['zmq_last_time']
|
||||||
|
});
|
||||||
|
|
||||||
function addLimited(target, message, maxCount) {
|
function addLimited(target, message, maxCount) {
|
||||||
target.unshift(message)
|
target.unshift(message)
|
||||||
if (target.length > maxCount) {
|
if (target.length > maxCount) {
|
||||||
|
|
Loading…
Reference in a new issue