websocket init
This commit is contained in:
@ -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 %}
|
Reference in New Issue
Block a user