Socket.IO teilt sich den HTTP-Port statt eines eigenen Servers auf 3001

Der Socket.IO-Server lief als zweiter Server auf Port 3001 und brauchte
deswegen cors: { origin: "*" }; die Clients bauten ihre Verbindungs-URL aus
window.location.hostname und dem festen Port zusammen.

Er wird jetzt ohne eigenen Port erzeugt und in bin/www per io.attach() an den
bestehenden HTTP-Server gehaengt. Erzeugen und Anhaengen sind getrennt, weil das
Modul beim Laden der Routen ausgewertet wird, also bevor der HTTP-Server
existiert. Die Clients rufen nur noch io() ohne Argument auf und verbinden sich
damit zur Herkunft der Seite zurueck.

Damit entfallen der zweite Port, die CORS-Ausnahme und eine moegliche zweite
Firewall-Regel auf dem Pi. Verifiziert: Port 3001 lauscht nicht mehr, alle
Events (score, timerDurationLeft, timerEnded, scoreSideswitch, refresh) kommen
ueber Port 3000 an, Reconnect funktioniert.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Julian Appel 2026-08-19 21:08:34 +02:00
parent cd4877bdd2
commit 2a1e0904e8
7 changed files with 36 additions and 17 deletions

View file

@ -7,6 +7,7 @@
var app = require('../app');
var debug = require('debug')('scoreboard:server');
var http = require('http');
var io = require('../controllers/socketio');
/**
* Get port from environment and store in Express.
@ -21,6 +22,12 @@ app.set('port', port);
var server = http.createServer(app);
/**
* Attach Socket.IO to the same server, so it shares the HTTP port.
*/
io.attach(server);
/**
* Listen on provided port, on all network interfaces.
*/

View file

@ -1,13 +1,15 @@
// Erstellt den zentralen Socket.IO-Server auf Port 3001.
// Erstellt den zentralen Socket.IO-Server.
// Wird von timer.js, score.js und den Routen importiert,
// um Events (Timer, Score, Refresh) an alle verbundenen Clients zu senden.
const { Server } = require('socket.io');
// CORS auf "*" gesetzt, da Admin und Monitor auf unterschiedlichen Ports laufen können
const io = new Server(3001, {
cors: { origin: "*" }
});
// Der Server wird ohne eigenen Port erzeugt und in bin/www per io.attach() an den
// HTTP-Server gehaengt. Dieses Modul wird beim Laden der Routen ausgewertet, also
// bevor der HTTP-Server existiert — daher die Trennung von Erzeugen und Anhaengen.
// Weil Socket.IO damit unter derselben Herkunft laeuft wie die Seite, entfaellt die
// CORS-Ausnahme.
const io = new Server();
// Verbindungs-Handling: der Client meldet sich beim Verbinden selbst mit dem
// Socket.IO-eigenen 'connect'-Event und holt sich den aktuellen Stand ueber die

View file

@ -1,5 +1,6 @@
let wss = "ws://" + window.location.hostname + ":3001" // Build socketio endpoint from window.location
const socket = io(wss); // Connect to socketio server
// Socket.IO laeuft am selben Host und Port wie die Seite, io() ohne Argument
// verbindet sich dorthin zurueck.
const socket = io();
// ######################################################################################################################################################
// Websockets event handler

View file

@ -1,5 +1,6 @@
let wss = "ws://" + window.location.hostname + ":3001" // Build socketio endpoint from window.location
const socket = io(wss); // Connect to socketio server
// Socket.IO laeuft am selben Host und Port wie die Seite, io() ohne Argument
// verbindet sich dorthin zurueck.
const socket = io();
// ######################################################################################################################################################
// Websockets event handler

View file

@ -1,5 +1,6 @@
let wss = "ws://" + window.location.hostname + ":3001" // Build socketio endpoint from window.location
const socket = io(wss); // Connect to socketio server
// Socket.IO laeuft am selben Host und Port wie die Seite, io() ohne Argument
// verbindet sich dorthin zurueck.
const socket = io();
// ######################################################################################################################################################
// Websockets event handler

View file

@ -1,5 +1,6 @@
let wss = "ws://" + window.location.hostname + ":3001" // Build socketio endpoint from window.location
const socket = io(wss); // Connect to socketio server
// Socket.IO laeuft am selben Host und Port wie die Seite, io() ohne Argument
// verbindet sich dorthin zurueck.
const socket = io();
// ######################################################################################################################################################
// Websockets event handler