Web: show SocketIO connection status

This commit is contained in:
Sybren A. Stüvel 2022-05-11 13:09:01 +02:00
parent 63ac728732
commit e9e1cd8be8
4 changed files with 83 additions and 1 deletions

View File

@ -54,6 +54,9 @@
--color-status-cancel-requested: hsl(194 30% 50%); --color-status-cancel-requested: hsl(194 30% 50%);
--color-status-under-construction: hsl(194 30% 50%); --color-status-under-construction: hsl(194 30% 50%);
--color-connection-lost-text: hsl(17, 65%, 65%);
--color-connection-lost-bg: hsl(17, 65%, 20%);
} }
html, html,

View File

@ -1,14 +1,44 @@
<template> <template>
<span class='notifications' v-if="notifs.last">{{ notifs.last.msg }}</span> <section class="notification-bar">
<span class='notifications' v-if="notifs.last">{{ notifs.last.msg }}</span>
<span class='socket-status' v-if="!sockStatus.isConnected" :title="sockStatus.message">Connection Lost</span>
</section>
</template> </template>
<script> <script>
import { useNotifs } from '@/stores/notifications'; import { useNotifs } from '@/stores/notifications';
import { useSocketStatus } from '@/stores/socket-status';
export default { export default {
name: 'NotificationBar', name: 'NotificationBar',
data: () => ({ data: () => ({
notifs: useNotifs(), notifs: useNotifs(),
sockStatus: useSocketStatus(),
}), }),
} }
</script> </script>
<style scoped>
.notification-bar {
width: 100%;
}
.socket-status {
float: right;
font-weight: bold;
font-size: larger;
min-width: 1.2em;
padding: 0.2em 1em;
border-radius: 0.5em;
text-align: center;
color: var(--color-connection-lost-text);
background-color: var(--color-connection-lost-bg);
box-shadow: 0 0 1em var(--color-connection-lost-bg);
border: solid thin var(--color-connection-lost-text);
cursor: help;
}
</style>

View File

@ -5,6 +5,8 @@
<script> <script>
import io from "socket.io-client"; import io from "socket.io-client";
import * as API from "@/manager-api" import * as API from "@/manager-api"
import { useSocketStatus } from '@/stores/socket-status';
export default { export default {
emits: [ emits: [
@ -17,6 +19,7 @@ export default {
data() { data() {
return { return {
socket: null, socket: null,
sockStatus: useSocketStatus(),
} }
}, },
mounted: function () { mounted: function () {
@ -57,20 +60,29 @@ export default {
// console. // console.
window.ws = ws; window.ws = ws;
this.socket.on('open', (error) => {
console.log("socketIO connection open");
this.sockStatus.connected();
});
this.socket.on('connect_error', (error) => { this.socket.on('connect_error', (error) => {
// Don't log the error here, it's too long and noisy for regular logs. // Don't log the error here, it's too long and noisy for regular logs.
console.log("socketIO connection error"); console.log("socketIO connection error");
this.sockStatus.disconnected(error);
}); });
this.socket.on('error', (error) => { this.socket.on('error', (error) => {
console.log("socketIO error:", error); console.log("socketIO error:", error);
this.sockStatus.disconnected(error);
}); });
this.socket.on('connect_timeout', (timeout) => { this.socket.on('connect_timeout', (timeout) => {
console.log("socketIO connection timeout:", timeout); console.log("socketIO connection timeout:", timeout);
this.sockStatus.disconnected("Connection timeout");
}); });
this.socket.on("disconnect", (reason) => { this.socket.on("disconnect", (reason) => {
console.log("socketIO disconnected:", reason); console.log("socketIO disconnected:", reason);
this.$emit("sioDisconnected", reason); this.$emit("sioDisconnected", reason);
this.sockStatus.disconnected(reason);
if (reason === 'io server disconnect') { if (reason === 'io server disconnect') {
// The disconnection was initiated by the server, need to reconnect // The disconnection was initiated by the server, need to reconnect
// manually. If the disconnect was for other reasons, the socket // manually. If the disconnect was for other reasons, the socket
@ -84,6 +96,7 @@ export default {
}); });
this.socket.on("reconnect", (attemptNumber) => { this.socket.on("reconnect", (attemptNumber) => {
console.log("socketIO reconnected after", attemptNumber, "attempts"); console.log("socketIO reconnected after", attemptNumber, "attempts");
this.sockStatus.connected();
// Resubscribe to whatever we want to be subscribed to: // Resubscribe to whatever we want to be subscribed to:
if (this.subscribedJob) this._updateJobSubscription("subscribe", this.subscribedJob); if (this.subscribedJob) this._updateJobSubscription("subscribe", this.subscribedJob);

View File

@ -0,0 +1,36 @@
import { stringifyStyle } from '@vue/shared'
import { defineStore } from 'pinia'
/**
* Status of the SocketIO/Websocket connection to Flamenco Manager.
*/
export const useSocketStatus = defineStore('socket-status', {
state: () => ({
/** @type { bool } */
isConnected: false,
/** @type {string} */
message: "",
}),
actions: {
/**
* Indicate the connection was lost.
* @param {string} reason
*/
disconnected(reason) {
this.$patch({
isConnected: false,
message: `${reason}`,
});
},
/**
* Indicate the connection is good.
*/
connected() {
this.$patch({
isConnected: true,
message: "",
});
},
}
})