Ein einfacher externer WebSocket-Server und in useLineData.js testen

This commit is contained in:
ISA
2024-09-11 08:58:01 +02:00
parent fb0e8b817d
commit f5e9de16f6
3 changed files with 66 additions and 59 deletions

View File

@@ -1,27 +0,0 @@
// /pages/api/websocket.js
import { Server } from "ws";
export default function handler(req, res) {
if (!res.socket.server.ws) {
console.log("Starting WebSocket server");
const wss = new Server({ server: res.socket.server });
wss.on("connection", (ws) => {
console.log("New WebSocket connection");
ws.on("message", (message) => {
console.log("Received:", message);
// Beispielnachricht an den Client senden
ws.send(JSON.stringify({ message: "Hallo von WebSocket-Server" }));
});
// Schließe die Verbindung
ws.on("close", () => {
console.log("WebSocket connection closed");
});
});
res.socket.server.ws = wss;
}
res.end();
}