-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebsocket_server.py
71 lines (56 loc) · 2.05 KB
/
websocket_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import asyncio
import json
import logging
import websockets
from song import Song
messages = []
class WebSocketServer:
def __init__(self, download_queue: asyncio.Queue):
self.download_queue = download_queue
self.clients = set()
async def register(self, websocket):
self.clients.add(websocket)
async def unregister(self, websocket):
self.clients.remove(websocket)
async def send_to_clients(self, message):
logging.debug(f"Sending {message}")
messages.append(message)
if self.clients:
websockets.broadcast(self.clients, json.dumps(message))
async def handler(self, websocket, path):
await self.register(websocket)
try:
async for message in websocket:
print(f"Received message from client: {message}")
except Exception as e:
logging.error(f"Error on websocket: {e}")
finally:
await self.unregister(websocket)
async def message_queue_consumer(self):
while True:
message = await self.message_queue.get()
await self.send_to_clients(message)
async def download_queue_consumer(self, i):
# TODO: not actually threaded
await self.send_to_clients({
"msg": f"[Downloader {i}] Ready",
"type": "log"
})
while True:
id = await self.download_queue.get()
await self.send_to_clients({
"msg": f"[Downloader {i}] Starting download for {id}",
"type": "log"
})
try:
song = await Song.download(id)
await self.send_to_clients({
"msg": f"[Downloader {i}] Successfully downloaded {song}",
"type": "log"
})
except Exception as e:
logging.exception("Download failed")
await self.send_to_clients({
"msg": f"[Downloader {i}] Download for {id} failed: {e}",
"type": "error"
})