Started planning websocket daemon

This commit is contained in:
benarc 2022-02-08 13:52:19 +00:00 committed by Tiago vasconcelos
parent 2c435ee9d9
commit 3f356f9de8
2 changed files with 68 additions and 14 deletions

View file

@ -1,5 +1,6 @@
from http import HTTPStatus from http import HTTPStatus
import asyncio
import asyncio
from fastapi import Request from fastapi import Request
from fastapi.param_functions import Query from fastapi.param_functions import Query
from fastapi.params import Depends from fastapi.params import Depends
@ -7,13 +8,14 @@ from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse from starlette.responses import HTMLResponse
from . import nostradmin_ext, nostr_renderer from . import nostradmin_ext, nostr_renderer
from fastapi import Request, WebSocket, WebSocketDisconnect
from lnbits.core.crud import update_payment_status from lnbits.core.crud import update_payment_status
from lnbits.core.models import User from lnbits.core.models import User
from lnbits.core.views.api import api_payment from lnbits.core.views.api import api_payment
from lnbits.decorators import check_user_exists from lnbits.decorators import check_user_exists
from .crud import get_nostrkeys
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")
@ -23,3 +25,67 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
return nostr_renderer().TemplateResponse( return nostr_renderer().TemplateResponse(
"nostradmin/index.html", {"request": request, "user": user.dict()} "nostradmin/index.html", {"request": request, "user": user.dict()}
) )
#####################################################################
#################### NOSTR WEBSOCKET THREAD #########################
##### THE QUEUE LOOP THREAD THING THAT LISTENS TO BUNCH OF ##########
### WEBSOCKET CONNECTIONS, STORING DATA IN DB/PUSHING TO FRONTEND ###
################### VIA updater() FUNCTION ##########################
#####################################################################
websocket_queue = asyncio.Queue(1000)
# while True:
async def nostr_subscribe():
return
# for the relays:
# async with websockets.connect("ws://localhost:8765") as websocket:
# for the public keys:
# await websocket.send("subscribe to events")
# await websocket.recv()
#####################################################################
################### LNBITS WEBSOCKET ROUTES #########################
#### HERE IS WHERE LNBITS FRONTEND CAN RECEIVE AND SEND MESSAGES ####
#####################################################################
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
async def connect(self, websocket: WebSocket, nostr_id: str):
await websocket.accept()
websocket.id = nostr_id
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def send_personal_message(self, message: str, nostr_id: str):
for connection in self.active_connections:
if connection.id == nostr_id:
await connection.send_text(message)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
@nostradmin_ext.websocket("/nostradmin/ws/{nostr_id}", name="copilot.websocket_by_id")
async def websocket_endpoint(websocket: WebSocket, copilot_id: str):
await manager.connect(websocket, nostr_id)
try:
while True:
data = await websocket.receive_text()
except WebSocketDisconnect:
manager.disconnect(websocket)
async def updater(nostr_id, message):
copilot = await get_copilot(nostr_id)
if not copilot:
return
await manager.send_personal_message(f"{message}", nostr_id)

View file

@ -25,18 +25,6 @@ from .crud import (
) )
from .models import nostrKeys, nostrCreateRelays, nostrRelaySetList from .models import nostrKeys, nostrCreateRelays, nostrRelaySetList
# while True:
async def nostr_subscribe():
return
# for the relays:
# async with websockets.connect("ws://localhost:8765") as websocket:
# for the public keys:
# await websocket.send("subscribe to events")
# await websocket.recv()
websocket_queue = asyncio.Queue(1000)
@nostradmin_ext.get("/api/v1/relays") @nostradmin_ext.get("/api/v1/relays")
async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)): async def api_relays_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):