Webserver hinzugefügt, Javascript angepasst abruf der IP Dynamisch
This commit is contained in:
parent
5f6915bd89
commit
9cbce7415d
2 changed files with 31 additions and 16 deletions
45
client.js
45
client.js
|
|
@ -1,19 +1,34 @@
|
||||||
console.log('Script Loaded')
|
/**
|
||||||
|
* @returns {Promise<{server_ip: string, server_port: number}>}
|
||||||
const ws = new WebSocket("ws://localhost:8000");
|
*/
|
||||||
ws.onopen = function (openEvent) {
|
async function getServerConfig() {
|
||||||
console.log("WebSocket ist mit dem " + ws.protocol + "Protokoll geöffnet");
|
const response = await fetch('/api/ip');
|
||||||
ws.send("Hallo Server");
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.onmessage = function (messageEvent) {
|
getServerConfig()
|
||||||
console.log(messageEvent.data);
|
.then(data => {
|
||||||
}
|
const websocketIp = data.server_ip;
|
||||||
|
const websocketPort = data.server_port;
|
||||||
|
const ws = new WebSocket(`ws://${websocketIp}:${websocketPort}`);
|
||||||
|
|
||||||
ws.onclose = function (closeEvent) {
|
ws.onopen = function() {
|
||||||
console.log("Websocket wurde geschlossen")
|
console.log("WebSocket ist geöffnet");
|
||||||
}
|
ws.send(JSON.stringify({"data_message": "Server Adresse: " + websocketIp + ":" + websocketPort}));
|
||||||
|
};
|
||||||
|
|
||||||
ws.onerror = function (errorEvent) {
|
ws.onmessage = function(messageEvent) {
|
||||||
console.error("Websocket-Fehler: " , errorEvent)
|
console.log(messageEvent.data);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
ws.onclose = function() {
|
||||||
|
console.log("WebSocket wurde geschlossen");
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onerror = function(errorEvent) {
|
||||||
|
console.error("WebSocket-Fehler: ", errorEvent);
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.error('Error fetching settings:', error);
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,6 @@
|
||||||
<title>Websocket Client</title>
|
<title>Websocket Client</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script src="client.js"></script>
|
<script src="/client/client.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in a new issue