commit b47b0d31c206af4732ee9b6041e08bc3506d47c1 Author: Oliver Hofmann Date: Mon May 12 19:31:33 2025 +0200 Initial diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bc0dbe0 --- /dev/null +++ b/Dockerfile @@ -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"] + diff --git a/package.json b/package.json new file mode 100644 index 0000000..22342c0 --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/volume/public/index.html b/volume/public/index.html new file mode 100644 index 0000000..1e81474 --- /dev/null +++ b/volume/public/index.html @@ -0,0 +1,16 @@ + + + + + + WebSocket Client + + +

WebSocket Client

+ + +
+ + + + diff --git a/volume/public/script.js b/volume/public/script.js new file mode 100644 index 0000000..1938352 --- /dev/null +++ b/volume/public/script.js @@ -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 = ''; +}); + diff --git a/volume/server.js b/volume/server.js new file mode 100644 index 0000000..b4a6c6b --- /dev/null +++ b/volume/server.js @@ -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}`); +}); +