mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-28 21:02:31 +02:00
Websocket fully working
This commit is contained in:
@@ -251,6 +251,7 @@
|
|||||||
}
|
}
|
||||||
this.connection = new WebSocket(localUrl)
|
this.connection = new WebSocket(localUrl)
|
||||||
this.connection.onmessage = function (e) {
|
this.connection.onmessage = function (e) {
|
||||||
|
console.log(e)
|
||||||
res = e.data.split('-')
|
res = e.data.split('-')
|
||||||
if (res[0] == 'rocket') {
|
if (res[0] == 'rocket') {
|
||||||
addTask(['40%', '/copilot/static/rocket.gif', res[1]])
|
addTask(['40%', '/copilot/static/rocket.gif', res[1]])
|
||||||
|
@@ -45,23 +45,23 @@ async def panel(request: Request):
|
|||||||
|
|
||||||
##################WEBSOCKET ROUTES########################
|
##################WEBSOCKET ROUTES########################
|
||||||
|
|
||||||
# socket_relay is a list where the control panel or
|
|
||||||
# lnurl endpoints can leave a message for the compose window
|
|
||||||
|
|
||||||
|
|
||||||
class ConnectionManager:
|
class ConnectionManager:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.active_connections: List[WebSocket] = []
|
self.active_connections: List[WebSocket] = []
|
||||||
|
|
||||||
async def connect(self, websocket: WebSocket):
|
async def connect(self, websocket: WebSocket, copilot_id: str):
|
||||||
await websocket.accept()
|
await websocket.accept()
|
||||||
|
websocket.id = copilot_id
|
||||||
self.active_connections.append(websocket)
|
self.active_connections.append(websocket)
|
||||||
|
|
||||||
def disconnect(self, websocket: WebSocket):
|
def disconnect(self, websocket: WebSocket):
|
||||||
self.active_connections.remove(websocket)
|
self.active_connections.remove(websocket)
|
||||||
|
|
||||||
async def send_personal_message(self, message: str, websocket: WebSocket):
|
async def send_personal_message(self, message: str, copilot_id: str):
|
||||||
await websocket.send_text(message)
|
for connection in self.active_connections:
|
||||||
|
if connection.id == copilot_id:
|
||||||
|
await connection.send_text(message)
|
||||||
|
|
||||||
async def broadcast(self, message: str):
|
async def broadcast(self, message: str):
|
||||||
for connection in self.active_connections:
|
for connection in self.active_connections:
|
||||||
@@ -71,21 +71,18 @@ class ConnectionManager:
|
|||||||
manager = ConnectionManager()
|
manager = ConnectionManager()
|
||||||
|
|
||||||
|
|
||||||
@copilot_ext.websocket("/copilot/ws/{socket_id}", name="copilot.websocket_by_id")
|
@copilot_ext.websocket("/copilot/ws/{copilot_id}", name="copilot.websocket_by_id")
|
||||||
async def websocket_endpoint(websocket: WebSocket, socket_id: str):
|
async def websocket_endpoint(websocket: WebSocket, copilot_id: str):
|
||||||
await manager.connect(websocket)
|
await manager.connect(websocket, copilot_id)
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
data = await websocket.receive_text()
|
data = await websocket.receive_text()
|
||||||
await manager.send_personal_message(f"You wrote: {data}", websocket)
|
|
||||||
await manager.broadcast(f"Client #{socket_id} says: {data}")
|
|
||||||
except WebSocketDisconnect:
|
except WebSocketDisconnect:
|
||||||
manager.disconnect(websocket)
|
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
|
||||||
manager.broadcast(f"{data + '-' + comment}")
|
await manager.send_personal_message(f"{data + '-' + comment}", copilot_id)
|
||||||
|
@@ -98,7 +98,6 @@ async def api_copilot_ws_relay(
|
|||||||
copilot_id: str = Query(None), comment: str = Query(None), data: str = Query(None)
|
copilot_id: str = Query(None), comment: 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, detail="Copilot does not exist"
|
status_code=HTTPStatus.NOT_FOUND, detail="Copilot does not exist"
|
||||||
|
Reference in New Issue
Block a user