2021-03-11 21:58:29 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>DEMO Websocket over HTML</title>
|
|
|
|
<link rel="icon" type="image/png" href="favicon.png">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
|
|
|
|
<meta name="author" content="Andros Fenollosa">
|
|
|
|
<meta name="generator" content="Django">
|
|
|
|
<meta name="keywords" content="html, css, javascript">
|
|
|
|
<meta name="description" content="my description...">
|
|
|
|
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css">
|
|
|
|
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-exp.min.css">
|
|
|
|
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-icons.min.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
2021-03-12 00:09:19 +01:00
|
|
|
<div class="container">
|
|
|
|
<nav id="nav">
|
|
|
|
<ul class="tab tab-block">
|
|
|
|
<li class="tab-item">
|
|
|
|
<a id="link-all" href="#">All</a>
|
|
|
|
</li>
|
|
|
|
<li class="tab-item">
|
|
|
|
<a id="link-about" href="#">About</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
<main id="main">
|
|
|
|
<div id="articles">
|
|
|
|
{% for post in posts %}
|
|
|
|
<article>
|
|
|
|
<h2>{{ post.title }}</h2>
|
|
|
|
<p>Author: {{ post.author }}</p>
|
|
|
|
<p><button class="btn">More</button></p>
|
|
|
|
<hr>
|
|
|
|
</article>
|
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
<ul class="pagination">
|
|
|
|
<li class="page-item disabled">
|
|
|
|
<a href="#" tabindex="-1">Previous</a>
|
|
|
|
</li>
|
|
|
|
<li class="page-item">
|
|
|
|
<a href="#">Next</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</main>
|
|
|
|
</div>
|
2021-03-11 21:58:29 +01:00
|
|
|
<script>
|
2021-03-12 00:09:19 +01:00
|
|
|
document.$CHAT_SOCKET = new WebSocket('ws://my-demo.localhost/ws/blog/{{ CHANNEL}}/');
|
2021-03-11 21:58:29 +01:00
|
|
|
|
|
|
|
// Conectado
|
2021-03-12 00:09:19 +01:00
|
|
|
document.$CHAT_SOCKET.addEventListener('open', () => {
|
|
|
|
console.log('Connect');
|
2021-03-11 21:58:29 +01:00
|
|
|
});
|
|
|
|
// Desconectado
|
2021-03-12 00:09:19 +01:00
|
|
|
document.$CHAT_SOCKET.addEventListener('close', () => {
|
|
|
|
console.log('Disconnect');
|
2021-03-11 21:58:29 +01:00
|
|
|
});
|
|
|
|
|
2021-03-12 00:09:19 +01:00
|
|
|
// Recibir mensaje y dibujar
|
|
|
|
document.$CHAT_SOCKET.addEventListener('message', (event) => {
|
|
|
|
console.log('New HTML');
|
|
|
|
const NEW_DATA = JSON.parse(event.data);
|
|
|
|
//document.querySelector(NEW_DATA.selector).innerHTML = NEW_DATA.html;
|
|
|
|
|
|
|
|
|
|
|
|
const rangeHTML = document.createRange().createContextualFragment(NEW_DATA.html);
|
|
|
|
document.querySelector(NEW_DATA.selector).innerHTML = '';
|
|
|
|
document.querySelector(NEW_DATA.selector).appendChild(rangeHTML);
|
|
|
|
|
|
|
|
|
|
|
|
console.log(rangeHTML)
|
2021-03-11 21:58:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2021-03-12 00:09:19 +01:00
|
|
|
document.querySelector('#link-about').addEventListener('click', (event) => {
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
document.$CHAT_SOCKET.send(JSON.stringify({
|
|
|
|
selector: "#main",
|
|
|
|
template: "partials/website/about.html",
|
|
|
|
data: {}
|
|
|
|
}));
|
|
|
|
});
|
2021-03-11 21:58:29 +01:00
|
|
|
|
2021-03-12 00:09:19 +01:00
|
|
|
document.querySelector('#link-all').addEventListener('click', (event) => {
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
document.$CHAT_SOCKET.send(JSON.stringify({
|
|
|
|
selector: "#main",
|
|
|
|
template: "partials/blog/all_articles.html",
|
|
|
|
data: {}
|
|
|
|
}));
|
|
|
|
});
|
2021-03-11 21:58:29 +01:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|