79 lines
1.8 KiB
JavaScript
79 lines
1.8 KiB
JavaScript
let websocket = null;
|
|
const messageBox = document.getElementById('messageBox');
|
|
|
|
window.addEventListener('load', function() {
|
|
connectWebSocket();
|
|
});
|
|
|
|
function connectWebSocket() {
|
|
const wsUrl = 'syslog';
|
|
console.log("Creating connection to: " + wsUrl)
|
|
websocket = new WebSocket(wsUrl);
|
|
|
|
websocket.onopen = function(event) {
|
|
console.log('WebSocket connected successfully');
|
|
};
|
|
|
|
websocket.onmessage = function(event) {
|
|
console.log('Message from server:', event.data);
|
|
messageBox.textContent += event.data + '\n';
|
|
messageBox.scrollTop = messageBox.scrollHeight;
|
|
};
|
|
}
|
|
|
|
const Channel = {
|
|
ONE: "one",
|
|
TWO: "two"
|
|
}
|
|
|
|
async function connectChannelOne() {
|
|
await connect(Channel.ONE);
|
|
}
|
|
|
|
async function connectChannelTwo() {
|
|
await connect(Channel.TWO);
|
|
}
|
|
|
|
async function connect(channel) {
|
|
var data = {};
|
|
|
|
if (channel === Channel.ONE) {
|
|
data = {
|
|
topic: document.getElementById('topic1').value,
|
|
address: document.getElementById('address1').value,
|
|
channel: Channel.ONE
|
|
}
|
|
}
|
|
|
|
if (channel === Channel.TWO) {
|
|
data = {
|
|
topic: document.getElementById('topic2').value,
|
|
address: document.getElementById('address2').value,
|
|
channel: Channel.TWO
|
|
}
|
|
}
|
|
await sendData(data, channel)
|
|
}
|
|
|
|
async function sendData(data, channel) {
|
|
const response = await fetch("webapp", {
|
|
method: "POST",
|
|
headers: {'Content-Type': 'application/json'},
|
|
body: JSON.stringify(data)
|
|
})
|
|
|
|
console.log(response)
|
|
const text = await response.text()
|
|
console.log(text)
|
|
|
|
if (response.status !== 200) {
|
|
displayError(channel)
|
|
return
|
|
}
|
|
|
|
//TODO websocket connection aufbauen
|
|
}
|
|
|
|
function displayError(channel) {
|
|
|
|
} |