Websockets not working
This commit is contained in:
parent
82a9cba871
commit
0c6cd94154
2 changed files with 32 additions and 16 deletions
|
|
@ -2,7 +2,6 @@ from http import HTTPStatus
|
||||||
import httpx
|
import httpx
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from lnbits.decorators import check_user_exists
|
from lnbits.decorators import check_user_exists
|
||||||
import asyncio
|
|
||||||
from .crud import get_copilot
|
from .crud import get_copilot
|
||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
@ -10,7 +9,7 @@ from functools import wraps
|
||||||
from lnbits.decorators import check_user_exists
|
from lnbits.decorators import check_user_exists
|
||||||
|
|
||||||
from . import copilot_ext, copilot_renderer
|
from . import copilot_ext, copilot_renderer
|
||||||
from fastapi import FastAPI, Request, WebSocket
|
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
|
||||||
from fastapi.params import Depends
|
from fastapi.params import Depends
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
from fastapi.param_functions import Query
|
from fastapi.param_functions import Query
|
||||||
|
|
@ -48,28 +47,44 @@ async def panel(request: Request):
|
||||||
# socket_relay is a list where the control panel or
|
# socket_relay is a list where the control panel or
|
||||||
# lnurl endpoints can leave a message for the compose window
|
# lnurl endpoints can leave a message for the compose window
|
||||||
|
|
||||||
connected_websockets = defaultdict(set)
|
|
||||||
|
|
||||||
|
class ConnectionManager:
|
||||||
|
def __init__(self):
|
||||||
|
self.active_connections: List[WebSocket] = []
|
||||||
|
|
||||||
@copilot_ext.websocket("/ws/{id}/")
|
async def connect(self, websocket: WebSocket):
|
||||||
async def websocket_endpoint(websocket: WebSocket, id: str = Query(None)):
|
|
||||||
copilot = await get_copilot(id)
|
|
||||||
if not copilot:
|
|
||||||
return "", HTTPStatus.FORBIDDEN
|
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
invoice_queue = asyncio.Queue()
|
self.active_connections.append(websocket)
|
||||||
connected_websockets[id].add(invoice_queue)
|
|
||||||
|
def disconnect(self, websocket: WebSocket):
|
||||||
|
self.active_connections.remove(websocket)
|
||||||
|
|
||||||
|
async def send_personal_message(self, message: str, websocket: WebSocket):
|
||||||
|
await websocket.send_text(message)
|
||||||
|
|
||||||
|
async def broadcast(self, message: str):
|
||||||
|
for connection in self.active_connections:
|
||||||
|
await connection.send_text(message)
|
||||||
|
|
||||||
|
|
||||||
|
manager = ConnectionManager()
|
||||||
|
|
||||||
|
|
||||||
|
@copilot_ext.websocket("/ws/{socket_id}")
|
||||||
|
async def websocket_endpoint(websocket: WebSocket, socket_id: str):
|
||||||
|
await manager.connect(websocket)
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data = await websocket.receive_text()
|
data = await websocket.receive_text()
|
||||||
await websocket.send_text(f"Message text was: {data}")
|
await manager.send_personal_message(f"You wrote: {data}", websocket)
|
||||||
finally:
|
await manager.broadcast(f"Client #{socket_id} says: {data}")
|
||||||
connected_websockets[id].remove(invoice_queue)
|
except WebSocketDisconnect:
|
||||||
|
manager.disconnect(websocket)
|
||||||
|
await manager.broadcast(f"Client #{socket_id} left the chat")
|
||||||
|
|
||||||
|
|
||||||
async def updater(copilot_id, data, comment):
|
async def updater(copilot_id, data, comment):
|
||||||
copilot = await get_copilot(copilot_id)
|
copilot = await get_copilot(copilot_id)
|
||||||
if not copilot:
|
if not copilot:
|
||||||
return
|
return
|
||||||
for queue in connected_websockets[copilot_id]:
|
manager.broadcast(f"{data + '-' + comment}")
|
||||||
await queue.send(f"{data + '-' + comment}")
|
|
||||||
|
|
|
||||||
|
|
@ -106,6 +106,7 @@ async def api_copilot_ws_relay(
|
||||||
data: str = Query(None),
|
data: str = Query(None),
|
||||||
):
|
):
|
||||||
copilot = await get_copilot(copilot_id)
|
copilot = await get_copilot(copilot_id)
|
||||||
|
print(copilot)
|
||||||
if not copilot:
|
if not copilot:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.NOT_FOUND,
|
status_code=HTTPStatus.NOT_FOUND,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue