diff --git a/test_examples/Websocket/index.html b/test_examples/Websocket/index.html new file mode 100644 index 0000000..f28e320 --- /dev/null +++ b/test_examples/Websocket/index.html @@ -0,0 +1,34 @@ + + + + + + WebSocket Example + + +

WebSocket Example

+ + + + + + + diff --git a/test_examples/Websocket/package-lock.json b/test_examples/Websocket/package-lock.json new file mode 100644 index 0000000..927638a --- /dev/null +++ b/test_examples/Websocket/package-lock.json @@ -0,0 +1,38 @@ +{ + "name": "Websocket_Test", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "http": "^0.0.1-security", + "ws": "^8.14.2" + } + }, + "node_modules/http": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/http/-/http-0.0.1-security.tgz", + "integrity": "sha512-RnDvP10Ty9FxqOtPZuxtebw1j4L/WiqNMDtuc1YMH1XQm5TgDRaR1G9u8upL6KD1bXHSp9eSXo/ED+8Q7FAr+g==" + }, + "node_modules/ws": { + "version": "8.14.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz", + "integrity": "sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + } + } +} diff --git a/test_examples/Websocket/package.json b/test_examples/Websocket/package.json new file mode 100644 index 0000000..3cd1b45 --- /dev/null +++ b/test_examples/Websocket/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "http": "^0.0.1-security", + "ws": "^8.14.2" + } +} diff --git a/test_examples/Websocket/server.js b/test_examples/Websocket/server.js new file mode 100644 index 0000000..07fab08 --- /dev/null +++ b/test_examples/Websocket/server.js @@ -0,0 +1,38 @@ +const http = require('http'); +const WebSocket = require('ws'); + +// Erstelle einen HTTP-Server +const server = http.createServer((req, res) => { + res.writeHead(200, {'Content-Type': 'text/plain'}); + res.end('WebSocket Server is running'); +}); + +// Erstelle einen WebSocket-Server, der an den HTTP-Server gebunden ist +const wss = new WebSocket.Server({ server }); + +// Event-Handler für eingehende WebSocket-Verbindungen +wss.on('connection', (ws) => { + console.log('Client connected'); + + // Event-Handler für eingehende Nachrichten vom Client + ws.on('message', (message) => { + client.send(`${message}`); + + // Sende die empfangene Nachricht an alle verbundenen Clients + wss.clients.forEach((client) => { + if (client !== ws && client.readyState === WebSocket.OPEN) { + client.send(message); + } + }); + }); + + // Event-Handler für das Schließen der WebSocket-Verbindung + ws.on('close', () => { + console.log('Client disconnected'); + }); +}); + +// Starte den Server auf Port 3000 +server.listen(3000, () => { + console.log('Server listening on http://localhost:3000'); +});