From 2ac9e75dac76abb8a034a7549bb5316fe1a9aad7 Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Wed, 14 Apr 2021 09:21:20 +0100 Subject: [PATCH] Adding websockets for panel (trying at least) --- .../copilot/templates/copilot/compose.html | 22 ++++++++++++++++++- lnbits/extensions/copilot/views.py | 11 +++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lnbits/extensions/copilot/templates/copilot/compose.html b/lnbits/extensions/copilot/templates/copilot/compose.html index c4d4f8b6..941838bf 100644 --- a/lnbits/extensions/copilot/templates/copilot/compose.html +++ b/lnbits/extensions/copilot/templates/copilot/compose.html @@ -57,7 +57,27 @@ mounted() { this.initCamera() }, - created: function () {} + created: function () { + console.log('{{ copilot.id }}') + + console.log('Starting connection to WebSocket Server') + this.connection = new WebSocket( + 'wss://' + + document.domain + + ':' + + location.port + + '/ws/{{ copilot.id }}' + ) + + this.connection.onmessage = function (event) { + console.log(event) + } + + this.connection.onopen = function (event) { + console.log(event) + console.log('Successfully connected to the echo websocket server...') + } + } }) {% endblock %} diff --git a/lnbits/extensions/copilot/views.py b/lnbits/extensions/copilot/views.py index e1bb4c54..8745b41d 100644 --- a/lnbits/extensions/copilot/views.py +++ b/lnbits/extensions/copilot/views.py @@ -1,4 +1,4 @@ -from quart import g, abort, render_template, jsonify +from quart import g, abort, render_template, jsonify, websocket from http import HTTPStatus from lnbits.decorators import check_user_exists, validate_uuids @@ -6,7 +6,6 @@ from lnbits.decorators import check_user_exists, validate_uuids from . import copilot_ext from .crud import get_copilot - @copilot_ext.route("/") @validate_uuids(["usr"], required=True) @check_user_exists() @@ -26,4 +25,10 @@ async def panel(copilot_id): copilot = await get_copilot(copilot_id) or abort( HTTPStatus.NOT_FOUND, "Copilot link does not exist." ) - return await render_template("copilot/panel.html", copilot=copilot) \ No newline at end of file + return await render_template("copilot/panel.html", copilot=copilot) + +@copilot_ext.websocket('/ws/') +async def ws(): + while True: + data = await websocket.receive() + await websocket.send(f"echo {data}") \ No newline at end of file