get_messages on handshake, send set_variables only to other users
This commit is contained in:
parent
a1106c4b22
commit
efc8f565bf
@ -25,7 +25,7 @@
|
|||||||
user: "testClient"+Math.random()
|
user: "testClient"+Math.random()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
setInterval(function() { setVariable('test','einTollerText'+Math.random()) }, 1000);
|
setInterval(function() { setVariable('test','einTollerText'+Math.random()) }, 5000);
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.onmessage = function(event) {
|
socket.onmessage = function(event) {
|
||||||
|
38
test.py
38
test.py
@ -3,6 +3,7 @@ import websockets
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
clients = {}
|
clients = {}
|
||||||
|
vars = {}
|
||||||
|
|
||||||
async def handshake(ws,j):
|
async def handshake(ws,j):
|
||||||
if not clients.get(j.get('project_id')):
|
if not clients.get(j.get('project_id')):
|
||||||
@ -11,10 +12,23 @@ async def handshake(ws,j):
|
|||||||
print(f"{j.get('user')} connected from {ws.host}:{ws.port}")
|
print(f"{j.get('user')} connected from {ws.host}:{ws.port}")
|
||||||
return(j.get('project_id'),j.get('user'))
|
return(j.get('project_id'),j.get('user'))
|
||||||
|
|
||||||
async def set_variable(j):
|
async def get_variables(websocket,project_id,user):
|
||||||
for name,ws in clients.get(j.get('project_id'),{}).items():
|
for name,value in vars.get(project_id,{}).items():
|
||||||
print(f"> {name} - {json.dumps(j)}")
|
if not name:
|
||||||
await ws.send(json.dumps(j))
|
continue
|
||||||
|
await websocket.send(json.dumps({"method":"set","name":name,"value":value}))
|
||||||
|
|
||||||
|
async def set_variable(project_id,user,var):
|
||||||
|
if not vars.get(project_id,''):
|
||||||
|
vars[project_id] = {}
|
||||||
|
vars[project_id][var.get('name','')] = var.get('value','')
|
||||||
|
|
||||||
|
data = json.dumps(var)
|
||||||
|
for name,ws in clients.get(project_id,{}).items():
|
||||||
|
if name == user:
|
||||||
|
continue
|
||||||
|
print(f"> {name} - {data}")
|
||||||
|
await ws.send(data)
|
||||||
|
|
||||||
async def disconnect(project_id,user):
|
async def disconnect(project_id,user):
|
||||||
print("disconnecting",user)
|
print("disconnecting",user)
|
||||||
@ -24,14 +38,16 @@ async def disconnect(project_id,user):
|
|||||||
async def process(websocket, path):
|
async def process(websocket, path):
|
||||||
project_id = ""
|
project_id = ""
|
||||||
user = ""
|
user = ""
|
||||||
async for data in websocket:
|
async for msg in websocket:
|
||||||
print(f"< {user} - {data}")
|
print(f"< {user} - {msg}")
|
||||||
try:
|
try:
|
||||||
j = json.loads(data)
|
data = json.loads(msg)
|
||||||
if j.get('method','') == 'handshake':
|
method = data.get('method','')
|
||||||
project_id,user = await handshake(websocket,j)
|
if method == 'handshake':
|
||||||
if j.get('method','') == 'set':
|
project_id,user = await handshake(websocket,data)
|
||||||
await set_variable(j)
|
await get_variables(websocket,project_id,user)
|
||||||
|
if method in ['set','create']:
|
||||||
|
await set_variable(project_id,user,data)
|
||||||
finally:
|
finally:
|
||||||
pass
|
pass
|
||||||
await disconnect(project_id,user)
|
await disconnect(project_id,user)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user