Upload files to "test_examples/Websocket"

main
Eyuep Sueyruege 2024-02-24 12:31:19 +01:00
parent 2f15ff5239
commit e184700b4d
4 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Example</title>
</head>
<body>
<h1>WebSocket Example</h1>
<input type="text" id="messageInput" placeholder="Type a message">
<button onclick="sendMessage()">Send</button>
<ul id="messages"></ul>
<script>
const socket = new WebSocket('ws://localhost:3000');
// Event-Handler für eingehende Nachrichten vom Server
socket.addEventListener('message', (event) => {
const messagesList = document.getElementById('messages');
const newMessage = document.createElement('li');
newMessage.textContent = event.data;
messagesList.appendChild(newMessage);
});
// Funktion zum Senden von Nachrichten
function sendMessage() {
const messageInput = document.getElementById('messageInput');
const message = messageInput.value;
socket.send(message);
messageInput.value = '';
}
</script>
</body>
</html>

View File

@ -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
}
}
}
}
}

View File

@ -0,0 +1,6 @@
{
"dependencies": {
"http": "^0.0.1-security",
"ws": "^8.14.2"
}
}

View File

@ -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');
});