152 lines
3.9 KiB
HTML
152 lines
3.9 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>MBC rc boatjes - commander</title>
|
|
<style>
|
|
* {
|
|
--background: #000000;
|
|
--background-second: #222222;
|
|
--foreground: #CCCCCC;
|
|
}
|
|
body {
|
|
background-color: var(--background);
|
|
color: var(--foreground);
|
|
}
|
|
.login {
|
|
position: fixed;
|
|
top: 50px;
|
|
right: 50px;
|
|
bottom: 50px;
|
|
left: 50px;
|
|
background-color: var(--background-second);
|
|
text-align: center;
|
|
padding: calc(50vh - 150px) 50px;
|
|
height: 200px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="login">
|
|
<input id="pass" type="password" value="1234" />
|
|
<button id="login">login</button>
|
|
</div>
|
|
<main>
|
|
<div id="boatjes"></div>
|
|
<div id="clients"></div>
|
|
</main>
|
|
<script>
|
|
const passEl = document.getElementById('pass');
|
|
const loginEl = document.getElementById('login');
|
|
const boatjesEl = document.getElementById('boatjes');
|
|
const clientsEl = document.getElementById('clients');
|
|
|
|
// const serverURL = 'ws://10.254.0.1:8080/';
|
|
const serverURL = 'ws://localhost:8080/';
|
|
|
|
var conn = new WebSocket(serverURL, ['mbcRcRf']);
|
|
|
|
loginEl.addEventListener('click', login)
|
|
|
|
function login(e)
|
|
{
|
|
conn.close();
|
|
conn = new WebSocket(serverURL, ['mbcRcRf']);
|
|
conn.addEventListener('open', onOpen);
|
|
conn.addEventListener('message', onMessage);
|
|
}
|
|
|
|
function onOpen(e)
|
|
{
|
|
conn.send(passEl.value);
|
|
}
|
|
|
|
function onMessage(e)
|
|
{
|
|
console.log("recive: ", e.data);
|
|
let msg = e.data.split(':')
|
|
let data = msg[0];
|
|
msg.splice(0, 1);
|
|
msg = msg.join(':');
|
|
switch (data)
|
|
{
|
|
case 'boats':
|
|
addBoats(msg);
|
|
passEl.parentElement.style.display = 'none';
|
|
break;
|
|
case 'clients':
|
|
addClients(msg);
|
|
break;
|
|
default:
|
|
console.warn('onMessage(): invlid data type (' + data + ')');
|
|
break;
|
|
}
|
|
}
|
|
|
|
function reqBoats()
|
|
{
|
|
conn.send(passEl.value + ";boats");
|
|
}
|
|
|
|
function addBoats(boats)
|
|
{
|
|
boats = boats.split(':');
|
|
for (let boat in boats)
|
|
{
|
|
if (boats[boat] != "")
|
|
{
|
|
addBoat(boats[boat]);
|
|
}
|
|
}
|
|
}
|
|
|
|
function addBoat(boat)
|
|
{
|
|
boat = boat.split(';')
|
|
if (boat.length == 3)
|
|
{
|
|
let boatEl = document.createElement("div");
|
|
boatEl.className = "boat";
|
|
boatEl.dataset['id'] = boat[0];
|
|
boatEl.dataset['availible'] = boat[2];
|
|
boatEl.innerText = boat[1];
|
|
boatjesEl.append(boatEl);
|
|
}
|
|
else
|
|
{
|
|
console.warn("addBoat(): incorect number of args (" + boat.length.toString() + "; " + boat.toString() + ")");
|
|
}
|
|
}
|
|
|
|
function addClients(clients)
|
|
{
|
|
clients = clients.split(':');
|
|
for (let client in clients)
|
|
{
|
|
if (clients[client] != "")
|
|
{
|
|
addClient(clients[client]);
|
|
}
|
|
}
|
|
}
|
|
|
|
function addClient(client)
|
|
{
|
|
client = client.split(';')
|
|
if (client.length == 2)
|
|
{
|
|
let clientEl = document.createElement("div");
|
|
clientEl.className = "client";
|
|
clientEl.dataset['id'] = client[0];
|
|
clientEl.innerText = client[0] + " controlls " + client[1];
|
|
clientsEl.append(clientEl);
|
|
}
|
|
else
|
|
{
|
|
console.warn("addBoat(): incorect number of args (" + client.length.toString() + "; " + client.toString() + ")");
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |