This commit is contained in:
Oliver Hofmann 2025-05-12 19:31:33 +02:00
commit b47b0d31c2
5 changed files with 148 additions and 0 deletions

24
Dockerfile Normal file
View File

@ -0,0 +1,24 @@
# Basis-Image
FROM node:22-alpine
# Verzeichnis anlegen
RUN mkdir -p /app/node_modules
# Arbeitsverzeichnis im Container
WORKDIR /app
# Nur package.json und lockfile kopieren
COPY package*.json ./
# Dependencies installieren
RUN npm install
# Volume definieren, damit der Code zur Laufzeit eingebunden wird
VOLUME ["/app/volume"]
# Port freigeben
EXPOSE 8080
# Standardbefehl (z.B. Start der App über bind mount)
CMD ["node", "volume/server.js"]

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "websocket-server",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Oliver Hofmann",
"license": "ISC",
"description": "",
"type": "module",
"dependencies": {
"ws": "^8.18.2"
}
}

16
volume/public/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Client</title>
</head>
<body>
<h1>WebSocket Client</h1>
<input type="text" id="message" placeholder="Type a message">
<button id="send">Send</button>
<div id="messages"></div>
<script src="node/script.js"></script>
</body>
</html>

23
volume/public/script.js Normal file
View File

@ -0,0 +1,23 @@
const ws = new WebSocket('wss://medinf.efi.th-nuernberg.de/node/ws');
ws.onopen = () => {
console.log('Connected to server');
};
ws.onmessage = (event) => {
const messages = document.getElementById('messages');
const newMessage = document.createElement('div');
newMessage.textContent = `Received: ${event.data}`;
messages.appendChild(newMessage);
};
ws.onclose = () => {
console.log('Disconnected from server');
};
document.getElementById('send').addEventListener('click', () => {
const input = document.getElementById('message');
ws.send(input.value);
input.value = '';
});

69
volume/server.js Normal file
View File

@ -0,0 +1,69 @@
import http from 'http';
import { WebSocketServer } from 'ws';
import fs from 'fs';
import path from 'path';
import url from 'url';
// 1. HTTP-Server erstellen
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url);
let pathname = `./volume/public${parsedUrl.pathname}`;
fs.exists(pathname, function (exist) {
if (!exist) {
res.statusCode = 404;
res.end(`File ${pathname} not found!`);
return;
}
// Wenn Pfad ein Verzeichnis ist, dann index.html annehmen
if (fs.statSync(pathname).isDirectory()) {
pathname += '/index.html';
}
fs.readFile(pathname, function (err, data) {
if (err) {
res.statusCode = 500;
res.end(`Error getting the file: ${err}.`);
} else {
res.statusCode = 200;
res.end(data);
}
});
});
});
// 2. WebSocket-Server auf dem HTTP-Server starten und auf /ws beschränken
const wss = new WebSocketServer({ noServer: true });
server.on('upgrade', (req, socket, head) => {
const pathname = url.parse(req.url).pathname;
if (pathname === '/ws') {
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
} else {
socket.destroy();
}
});
// 3. WebSocket-Handler
wss.on('connection', (socket) => {
console.log('Client connected to /ws');
socket.on('message', (message) => {
console.log(`Received: ${message}`);
socket.send(`Server: ${message}`);
});
socket.on('close', () => {
console.log('Client disconnected');
});
});
const PORT = 8080;
server.listen(PORT, () => {
console.log(`HTTP and WebSocket server running on http://localhost:${PORT}`);
});