patch MQTT

This commit is contained in:
2026-02-01 13:53:36 +01:00
parent bcec23a751
commit 0244854ddb

View File

@@ -59,7 +59,7 @@
</template> </template>
<script setup> <script setup>
import { ref, reactive, onMounted } from 'vue'; import { ref, reactive, onMounted, onUnmounted } from 'vue';
import mqtt from 'mqtt'; import mqtt from 'mqtt';
import config from '@/config.js'; // Ensure correct path import config from '@/config.js'; // Ensure correct path
@@ -72,34 +72,45 @@
Green: { Total: 0, Round: 0 }, Green: { Total: 0, Round: 0 },
}); });
const client = mqtt.connect(config.mqttBrokerUrl); // const client = mqtt.connect(config.mqttBrokerUrl);
let client = null;
client.on('connect', () => { onMounted(() => {
console.log('CardButtonScore: Connected to MQTT broker at', config.mqttBrokerUrl); client = mqtt.connect(config.mqttBrokerUrl);
client.subscribe('game/score');
client.on('connect', () => {
console.log('CardButtonScore: Connected to MQTT broker at', config.mqttBrokerUrl);
client.subscribe('game/score');
});
client.on('error', (err) => {
console.error('CardButtonScore: MQTT Error:', err);
});
client.on('message', (topic, message) => {
if (topic === 'game/score') {
try {
const data = JSON.parse(message.toString());
console.log('CardButtonScore: Received score update:', data);
if (data && data.TEAM) {
Object.keys(scores).forEach(color => {
if (data.TEAM[color]) {
scores[color].Total = data.TEAM[color].TotalScore;
scores[color].Round = data.TEAM[color].RoundScore;
}
});
}
} catch (e) {
console.error("Error parsing score update:", e);
}
}
});
}); });
client.on('error', (err) => { onUnmounted(() => {
console.error('CardButtonScore: MQTT Error:', err); if (client) {
}); client.end();
}
client.on('message', (topic, message) => {
if (topic === 'game/score') {
try {
const data = JSON.parse(message.toString());
console.log('CardButtonScore: Received score update:', data);
if (data && data.TEAM) {
Object.keys(scores).forEach(color => {
if (data.TEAM[color]) {
scores[color].Total = data.TEAM[color].TotalScore;
scores[color].Round = data.TEAM[color].RoundScore;
}
});
}
} catch (e) {
console.error("Error parsing score update:", e);
}
}
}); });
function toggleCardSize() { function toggleCardSize() {