socket.io und mock Data
This commit is contained in:
@@ -86,7 +86,7 @@ import { io } from "socket.io-client";
|
|||||||
const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
|
const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
|
||||||
//-------------------------------
|
//-------------------------------
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
useDataUpdater();
|
// useDataUpdater();
|
||||||
|
|
||||||
const countdown = useSelector(state => state.polylineContextMenu.countdown);
|
const countdown = useSelector(state => state.polylineContextMenu.countdown);
|
||||||
const countdownActive = useSelector(state => state.polylineContextMenu.countdownActive);
|
const countdownActive = useSelector(state => state.polylineContextMenu.countdownActive);
|
||||||
@@ -798,23 +798,31 @@ const MapComponent = ({ locations, onLocationUpdate, lineCoordinates }) => {
|
|||||||
//--------------------------------------------
|
//--------------------------------------------
|
||||||
|
|
||||||
useEffect(() => {
|
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", () => {
|
socket.on("connect", () => {
|
||||||
console.log("🔗 Verbunden mit dem Socket.io Server");
|
console.log("🔗 Socket verbunden", socket.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on("message", data => {
|
socket.on("gisLinesStatusUpdated", newData => {
|
||||||
console.log("📨 Nachricht vom Server:", data);
|
console.log("📡 Update erhalten für m/u:", m, u);
|
||||||
|
dispatch(fetchGisLinesStatusThunk(newData));
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.emit("message", { event: "ping", page: window.location.pathname });
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socket.disconnect();
|
socket.disconnect();
|
||||||
console.log("🔌 Verbindung getrennt");
|
|
||||||
};
|
};
|
||||||
}, []);
|
}, [dispatch]);
|
||||||
|
|
||||||
//---------------------------------------------
|
//---------------------------------------------
|
||||||
//--------------------------------------------
|
//--------------------------------------------
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
// /config/appVersion
|
// /config/appVersion
|
||||||
export const APP_VERSION = "1.1.243";
|
export const APP_VERSION = "1.1.244";
|
||||||
|
|||||||
36
server.js
36
server.js
@@ -3,6 +3,8 @@ const express = require("express");
|
|||||||
const http = require("http");
|
const http = require("http");
|
||||||
const next = require("next");
|
const next = require("next");
|
||||||
const { Server } = require("socket.io");
|
const { Server } = require("socket.io");
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
const port = parseInt(process.env.PORT, 10) || 3000;
|
const port = parseInt(process.env.PORT, 10) || 3000;
|
||||||
const dev = process.env.NODE_ENV !== "production";
|
const dev = process.env.NODE_ENV !== "production";
|
||||||
@@ -14,7 +16,7 @@ app.prepare().then(() => {
|
|||||||
const server = http.createServer(expressApp);
|
const server = http.createServer(expressApp);
|
||||||
const io = new Server(server);
|
const io = new Server(server);
|
||||||
|
|
||||||
// WebSocket-Logik
|
// Verbindung mit Clients
|
||||||
io.on("connection", socket => {
|
io.on("connection", socket => {
|
||||||
console.log("✅ Client verbunden via socket.io");
|
console.log("✅ Client verbunden via socket.io");
|
||||||
|
|
||||||
@@ -23,13 +25,43 @@ app.prepare().then(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.emit("message", { message: "Hallo vom socket.io Server" });
|
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) => {
|
expressApp.all("*", (req, res) => {
|
||||||
return handle(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, () => {
|
server.listen(port, () => {
|
||||||
console.log(`🚀 Server mit socket.io läuft auf http://localhost:${port}`);
|
console.log(`🚀 Server mit socket.io läuft auf http://localhost:${port}`);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// /services/webservice/fetchGisLinesStatusService.js
|
||||||
export const fetchGisLinesStatusService = async () => {
|
export const fetchGisLinesStatusService = async () => {
|
||||||
const useMocks = process.env.NEXT_PUBLIC_USE_MOCKS === "true";
|
const useMocks = process.env.NEXT_PUBLIC_USE_MOCKS === "true";
|
||||||
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";
|
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";
|
||||||
|
|||||||
Reference in New Issue
Block a user