socket.io und mock Data

This commit is contained in:
Ismail Ali
2025-06-07 09:12:58 +02:00
parent a55b5b0189
commit 17def7357c
4 changed files with 53 additions and 12 deletions

View File

@@ -86,7 +86,7 @@ import { io } from "socket.io-client";
const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
//-------------------------------
const dispatch = useDispatch();
useDataUpdater();
// useDataUpdater();
const countdown = useSelector(state => state.polylineContextMenu.countdown);
const countdownActive = useSelector(state => state.polylineContextMenu.countdownActive);
@@ -798,23 +798,31 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
//--------------------------------------------
useEffect(() => {
const socket = io();
const params = new URLSearchParams(window.location.search);
const m = params.get("m");
const u = params.get("u");
const socket = io({
query: {
m,
u,
},
});
socket.on("connect", () => {
console.log("🔗 Verbunden mit dem Socket.io Server");
console.log("🔗 Socket verbunden", socket.id);
});
socket.on("message", data => {
console.log("📨 Nachricht vom Server:", data);
socket.on("gisLinesStatusUpdated", newData => {
console.log("📡 Update erhalten für m/u:", m, u);
dispatch(fetchGisLinesStatusThunk(newData));
});
socket.emit("message", { event: "ping", page: window.location.pathname });
return () => {
socket.disconnect();
console.log("🔌 Verbindung getrennt");
};
}, []);
}, [dispatch]);
//---------------------------------------------
//--------------------------------------------
return (

View File

@@ -1,2 +1,2 @@
// /config/appVersion
export const APP_VERSION = "1.1.243";
export const APP_VERSION = "1.1.244";

View File

@@ -3,6 +3,8 @@ const express = require("express");
const http = require("http");
const next = require("next");
const { Server } = require("socket.io");
const fs = require("fs");
const path = require("path");
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
@@ -14,7 +16,7 @@ app.prepare().then(() => {
const server = http.createServer(expressApp);
const io = new Server(server);
// WebSocket-Logik
// Verbindung mit Clients
io.on("connection", socket => {
console.log("✅ Client verbunden via socket.io");
@@ -23,13 +25,43 @@ app.prepare().then(() => {
});
socket.emit("message", { message: "Hallo vom socket.io Server" });
//---------
const { m, u } = socket.handshake.query;
console.log(`🧩 WebSocket-Client verbunden mit Parametern: m=${m}, u=${u}`);
// Du kannst diese Info verwenden, um gezielt nur bestimmte Daten zu senden:
socket.join(`map-${m}-user-${u}`);
});
// Next.js Routen
// 🌍 Next.js Routing
expressApp.all("*", (req, res) => {
return handle(req, res);
});
// 📁 Datei überwachen: GisLinesStatus.json (Pfad anpassen falls nötig)
const statusFilePath = path.join(__dirname, "mockData", "GisLinesStatus.json");
fs.watchFile(statusFilePath, { interval: 5000 }, async () => {
console.log("📁 Änderung erkannt: GisLinesStatus.json");
try {
const content = fs.readFileSync(statusFilePath, "utf8");
const data = JSON.parse(content);
if (Array.isArray(data.Statis)) {
io.emit("gisLinesStatusUpdated", data.Statis);
console.log("📤 Update gesendet an Clients via Socket.IO");
} else {
console.warn("⚠️ 'Statis' fehlt oder hat falsches Format");
}
} catch (e) {
console.error("❌ Fehler beim Parsen von GisLinesStatus.json:", e.message);
}
});
// 🚀 Server starten
server.listen(port, () => {
console.log(`🚀 Server mit socket.io läuft auf http://localhost:${port}`);
});

View File

@@ -1,3 +1,4 @@
// /services/webservice/fetchGisLinesStatusService.js
export const fetchGisLinesStatusService = async () => {
const useMocks = process.env.NEXT_PUBLIC_USE_MOCKS === "true";
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";