41 lines
1,021 B
Python
41 lines
1,021 B
Python
import asyncio
|
|
import os
|
|
|
|
import websockets
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
CONNECTIONS = set()
|
|
EAS_MESSAGE = ""
|
|
TOKEN = os.getenv("TOKEN")
|
|
|
|
|
|
async def handle(websocket):
|
|
global EAS_MESSAGE
|
|
# register clients
|
|
if websocket not in CONNECTIONS:
|
|
CONNECTIONS.add(websocket)
|
|
async for message in websocket:
|
|
if message == "GET":
|
|
if EAS_MESSAGE == "":
|
|
await websocket.send("NULL")
|
|
else:
|
|
await websocket.send(f"MSG {EAS_MESSAGE}")
|
|
if message.startswith(f"MSG {TOKEN} "):
|
|
# broadcast from vee
|
|
EAS_MESSAGE = message.replace(f"MSG {TOKEN} ", "")
|
|
websockets.broadcast(CONNECTIONS, f"MSG {EAS_MESSAGE}")
|
|
if message.startswith(f"CLEAR {TOKEN}"):
|
|
# clear
|
|
EAS_MESSAGE = ""
|
|
await websocket.send(f"OK")
|
|
|
|
|
|
async def main():
|
|
async with websockets.serve(handle, "0.0.0.0", 8765):
|
|
await asyncio.Future() # run forever
|
|
|
|
|
|
asyncio.run(main())
|