63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
import asyncio
|
|
import json
|
|
import logging
|
|
import websockets
|
|
|
|
clients = {}
|
|
vars = {}
|
|
|
|
logging.basicConfig(
|
|
format="%(asctime)s %(message)s",
|
|
level=logging.INFO
|
|
)
|
|
|
|
async def handshake(ws,data):
|
|
project_id = data.get('project_id','')
|
|
user = data.get('user')
|
|
logging.info(f"{project_id} new user connected from {ws.host}:{ws.port}")
|
|
|
|
if not clients.get(project_id):
|
|
clients[project_id] = []
|
|
clients[project_id].append(ws)
|
|
await get_variables(ws,project_id,user)
|
|
return (project_id,user)
|
|
|
|
async def get_variables(ws,project_id,user):
|
|
for name,value in vars.get(project_id,{}).items():
|
|
if not name:
|
|
continue
|
|
await ws.send(json.dumps({"method":"set","name":name,"value":value}))
|
|
|
|
async def set_variable(project_id,var):
|
|
if not vars.get(project_id,''):
|
|
vars[project_id] = {}
|
|
vars[project_id][var.get('name','')] = var.get('value','')
|
|
websockets.broadcast(clients.get(project_id,[]),json.dumps(var))
|
|
|
|
async def disconnect(project_id,ws):
|
|
logging.info(f"disconnecting {ws.host}:{ws.port}")
|
|
clients.get(project_id,[]).remove(ws)
|
|
|
|
async def handler(ws, path):
|
|
project_id = ""
|
|
user = ""
|
|
async for msg in ws:
|
|
logging.info(f"< {user} - {msg}")
|
|
try:
|
|
data = json.loads(msg)
|
|
method = data.get('method','')
|
|
if method == 'handshake':
|
|
project_id,user = await handshake(ws,data)
|
|
if method in ['set','create']:
|
|
await set_variable(project_id,data)
|
|
finally:
|
|
pass
|
|
await disconnect(project_id,ws)
|
|
|
|
async def main():
|
|
async with websockets.serve(handler, "", 9080, compression=None):
|
|
await asyncio.Future() # run forever
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |