websocket init

This commit is contained in:
2024-03-15 15:44:29 +01:00
parent 11c6d4635f
commit 13c9c8bd3d
15 changed files with 332 additions and 25 deletions

View File

@ -1,15 +1,20 @@
{% extends 'base.html.twig' %}
{% block body %}
{{wssurl}}
<div id="chat" class="text-center mt-5">
<div class="mb-2">online</div>
<div id="online" style="mt-2"></div>
</div>
{% endblock %}
{% block localjavascript %}
{% if wssuse %}
<script>
var conn;
function connect() {
conn = new WebSocket("{{wssurl}}");
conn.onopen = function(e) {
console.log("== CONNECT");
{% set userkey = "" %}
@ -20,13 +25,14 @@
subscribe("home",1,"{{userkey}}");
sendMessage({command: "alive"});
};
conn.onmessage = function(e) {
ret=JSON.parse(e.data);
console.log(ret.log);
switch(ret.command) {
case "alive" :
if(!$('#online'+ret.from.id).length) {
console.log(ret);
html='<img id="online'+ret.from.id+'" src="'+ret.from.avatar+'" class="avatar ml-2 mr-2" title="'+ret.from.displayname+'">';
$("#online").append(html);
}
@ -57,5 +63,5 @@
}
connect();
</script>
{% endif %}
{% endif %}
{% endblock %}