Explode
This commit is contained in:
parent
4c01783828
commit
a7772fe8bc
1 changed files with 37 additions and 0 deletions
37
main.py
Normal file
37
main.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import asyncio
|
||||
import websockets
|
||||
|
||||
CONNECTIONS = set()
|
||||
EAS_MESSAGE = ""
|
||||
TOKEN = "o6LD27y63M0b360Pjd5B"
|
||||
|
||||
|
||||
async def handle(websocket):
|
||||
global EAS_MESSAGE
|
||||
# register clients
|
||||
if websocket not in CONNECTIONS:
|
||||
CONNECTIONS.add(websocket)
|
||||
async for message in websocket:
|
||||
# parse message
|
||||
if message == "HI":
|
||||
# new client said hi
|
||||
if message == "":
|
||||
await websocket.send("HI")
|
||||
else:
|
||||
await websocket.send(f"HI {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())
|
Reference in a new issue