From 6dceb61ba81834fedffe64588c74a84a553633ba Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Wed, 14 Apr 2021 17:00:18 +0100 Subject: [PATCH] Very hacky checker for websockets --- .../copilot/templates/copilot/compose.html | 91 +++++++++++++++---- .../copilot/templates/copilot/panel.html | 69 ++++++++++---- lnbits/extensions/copilot/views.py | 20 +++- 3 files changed, 140 insertions(+), 40 deletions(-) diff --git a/lnbits/extensions/copilot/templates/copilot/compose.html b/lnbits/extensions/copilot/templates/copilot/compose.html index 322dcf85d..def19107a 100644 --- a/lnbits/extensions/copilot/templates/copilot/compose.html +++ b/lnbits/extensions/copilot/templates/copilot/compose.html @@ -1,18 +1,18 @@ -{% extends "public.html" %} {% block page %}fdgasdf - +{% extends "public.html" %} {% block page %} + - - + {% endblock %} {% block scripts %} @@ -39,6 +39,10 @@ return {} }, methods: { + openURL: function (url) { + console.log(url) + return Quasar.utils.openURL(url) + }, initCamera() { var video = document.querySelector('#videoElement') @@ -52,6 +56,14 @@ console.log('Something went wrong!') }) } + }, + animation1: function () { + self = this + setTimeout(function () { + setInterval(function () { + self.connection.send('') + }, 1000) + }, 2000) } }, mounted() { @@ -60,19 +72,60 @@ created: function () { console.log('{{ copilot.id }}') - console.log('Starting connection to WebSocket Server') this.connection = new WebSocket( - 'wss://' + document.domain + ':' + location.port + '/ws' + 'ws://' + + document.domain + + ':' + + location.port + + '/copilot/ws/compose/{{ copilot.id }}' ) + this.connection.addEventListener('open', function (event) { + this.connection.send('') + }) - this.connection.onmessage = function (event) { - console.log(event) - } + this.connection.addEventListener('message', function (event) { + res = event.data.split('-') - this.connection.onopen = function (event) { - console.log(event) - console.log('Successfully connected to the echo websocket server...') - } + if (res[0] != this.oldRes) { + this.oldRes = res[0] + if (res[1] == 'animation1') { + document.getElementById('animations').style.width = '100%' + document.getElementById('animations').src = + '/copilot/static/confetti.gif' + setTimeout(function () { + document.getElementById('animations').src = '' + }, 5000) + } + if (res[1] == 'animation2') { + document.getElementById('animations').style.width = '50%' + document.getElementById('animations').src = + '/copilot/static/face.gif' + setTimeout(function () { + document.getElementById('animations').src = '' + }, 5000) + } + if (res[1] == 'animation3') { + document.getElementById('animations').style.width = '50%' + document.getElementById('animations').src = + '/copilot/static/rocket.gif' + setTimeout(function () { + document.getElementById('animations').src = '' + }, 5000) + } + if (res[1] == 'true') { + document.getElementById('videoElement').style.width = '10%' + } + if (res[1] == 'false') { + document.getElementById('videoElement').style.width = '100%' + } + } + }) + + this.connection.addEventListener('close', function (event) { + console.log('The connection has been closed') + }) + var animation1 = this.animation1 + animation1() } }) diff --git a/lnbits/extensions/copilot/templates/copilot/panel.html b/lnbits/extensions/copilot/templates/copilot/panel.html index a3b30ea63..07a03852e 100644 --- a/lnbits/extensions/copilot/templates/copilot/panel.html +++ b/lnbits/extensions/copilot/templates/copilot/panel.html @@ -24,7 +24,8 @@
- +
- +
- +
@@ -65,9 +78,6 @@ clearable type="textarea" label="Notes" - :shadow-text="textareaShadowText" - @keydown="processTextareaFill" - @focus="processTextareaFill" >
@@ -93,24 +103,47 @@ mixins: [windowMixin], data() { return { - newProgress: 0.4, - counter: 1, - newTimeLeft: '', - lnbtc: true, - onbtc: false, - formDialogCopilot: { - fullscreen_cam: true - } + fullscreen_cam: true, + textareaModel: '', + iframe: '' } }, methods: { + fullscreenToggle: function () { + this.connection.send(String(fullscreen_cam)) + }, openCompose: function () { let params = `scrollbars=no,resizable=no,status=no,location=no,toolbar=no,menubar=no,width=900,height=500,left=200,top=200` open('../copilot/cp/{{ copilot.id }}', 'test', params) + }, + animationBTN: function (name) { + this.connection.send(name) + }, + stfu: function (name) { + this.connection.send('') } }, - created: function () {} + created: function () { + console.log('{{ copilot.id }}') + + this.connection = new WebSocket( + 'ws://' + + document.domain + + ':' + + location.port + + '/copilot/ws/panel/{{ copilot.id }}' + ) + this.connection.addEventListener('open', function (event) {}) + + this.connection.addEventListener('message', function (event) { + console.log('Message from server ', event.data) + }) + + this.connection.addEventListener('close', function (event) { + console.log('The connection has been closed') + }) + } }) {% endblock %} diff --git a/lnbits/extensions/copilot/views.py b/lnbits/extensions/copilot/views.py index 2fa9ac849..2a2b4afc3 100644 --- a/lnbits/extensions/copilot/views.py +++ b/lnbits/extensions/copilot/views.py @@ -6,11 +6,25 @@ from lnbits.decorators import check_user_exists, validate_uuids from . import copilot_ext from .crud import get_copilot -@copilot_ext.websocket('/ws') -async def ws(): +from quart import g, abort, render_template, jsonify, websocket +from functools import wraps +import trio +import shortuuid +connected_websockets = {} + +@copilot_ext.websocket('/ws/panel/') +async def ws_panel(copilot_id): + global connected_websockets while True: data = await websocket.receive() - await websocket.send(f"echo {data}") + connected_websockets[copilot_id] = shortuuid.uuid() + '-' + data + +@copilot_ext.websocket('/ws/compose/') +async def ws_compose(copilot_id): + global connected_websockets + while True: + data = await websocket.receive() + await websocket.send(connected_websockets[copilot_id]) @copilot_ext.route("/") @validate_uuids(["usr"], required=True)