Upload files to "/"
This commit is contained in:
commit
f13b5c19ce
1 changed files with 40 additions and 0 deletions
40
ws.py
Normal file
40
ws.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import json
|
||||
import asyncio
|
||||
import websockets
|
||||
|
||||
true = True
|
||||
false = False
|
||||
null = None
|
||||
|
||||
clients = set()
|
||||
mtserver = null
|
||||
|
||||
async def serve(websocket):
|
||||
global mtserver
|
||||
clients.add(websocket)
|
||||
try:
|
||||
async for message in websocket:
|
||||
try:
|
||||
print(f"Received: {message}")
|
||||
data = json.loads(message)
|
||||
if data["type"] == "chat":
|
||||
for c in clients:
|
||||
if c != websocket: await c.send(json.dumps({"type": "chat", "name": data["sender"], "msg": data["msg"]}))
|
||||
elif data["type"] == "online":
|
||||
mtserver = websocket
|
||||
for c in clients:
|
||||
if c != mtserver: await c.send(json.dumps({"type": "online"}))
|
||||
else:
|
||||
pass
|
||||
print(f"Sent: {message}")
|
||||
except:
|
||||
continue
|
||||
finally:
|
||||
if mtserver == websocket: mtserver = null
|
||||
clients.remove(websocket)
|
||||
|
||||
async def main():
|
||||
server = await websockets.serve(serve, "", 8001)
|
||||
await server.wait_closed()
|
||||
|
||||
asyncio.run(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue