kopia lustrzana https://github.com/cirospaciari/socketify.py
47 wiersze
1.2 KiB
Python
47 wiersze
1.2 KiB
Python
![]() |
import falcon.asgi
|
||
|
import falcon.media
|
||
|
import asyncio
|
||
|
|
||
|
clients = set([])
|
||
|
remaining_clients = 16
|
||
|
|
||
|
async def broadcast(message):
|
||
|
|
||
|
# tasks = [ws.send_text(message) for ws in client]
|
||
|
# return await asyncio.wait(tasks, return_when=ALL_COMPLETED)
|
||
|
for ws in clients:
|
||
|
await ws.send_text(message)
|
||
|
# # for ws in clients:
|
||
|
# # tasks.append(ws.send_text(message))
|
||
|
# await asyncio.wait(tasks, return_when=ALL_COMPLETED)
|
||
|
|
||
|
|
||
|
class SomeResource:
|
||
|
|
||
|
async def on_get(self, req):
|
||
|
pass
|
||
|
|
||
|
async def on_websocket(self, req, ws):
|
||
|
global remaining_clients
|
||
|
try:
|
||
|
await ws.accept()
|
||
|
clients.add(ws)
|
||
|
remaining_clients = remaining_clients - 1
|
||
|
if remaining_clients == 0:
|
||
|
await broadcast("ready")
|
||
|
|
||
|
while True:
|
||
|
payload = await ws.receive_text()
|
||
|
await broadcast(payload)
|
||
|
|
||
|
except falcon.WebSocketDisconnected:
|
||
|
clients.remove(ws)
|
||
|
remaining_clients = remaining_clients + 1
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
app = falcon.asgi.App()
|
||
|
app.add_route('/', SomeResource())
|
||
|
# python3 -m gunicorn falcon_server:app -b 127.0.0.1:4001 -w 1 -k uvicorn.workers.UvicornWorker
|