Ramesh-vani commited on
Commit
7d45aaa
·
verified ·
1 Parent(s): e09f09f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -14,7 +14,6 @@ async def handle_client(websocket, path):
14
  try:
15
  # Receive the client's request (create a pair or connect to a key)
16
  request_type = await websocket.recv()
17
- print(request_type)
18
 
19
  if request_type == "create_pair":
20
  # Generate a unique key
@@ -31,17 +30,17 @@ async def handle_client(websocket, path):
31
  # Receive the key from the client
32
  key_to_connect = await websocket.recv()
33
 
34
- # Retrieve all clients associated with the key from the database
35
  cursor.execute('SELECT client_ws FROM pairs WHERE key=?', (key_to_connect,))
36
- results = cursor.fetchall()
 
 
 
 
 
 
 
37
 
38
- if results:
39
- # Pair the current client with all clients associated with the key
40
- for result in results:
41
- other_client_ws = result[0]
42
- if other_client_ws != str(websocket):
43
- await websocket.send(f"Connected to key {key_to_connect}. You are paired with {other_client_ws}")
44
- await other_client_ws.send(f"Connected to key {key_to_connect}. You are paired with {str(websocket)}")
45
  else:
46
  # Inform the client that the key does not exist
47
  await websocket.send(f"Key {key_to_connect} does not exist.")
@@ -53,7 +52,7 @@ def generate_key():
53
  # Generate a random key (you might want to implement a more robust key generation logic)
54
  return ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
55
 
56
- start_server = websockets.serve(handle_client, "0.0.0.0", 7860)
57
 
58
  async def main():
59
  print("WebSocket server is running. Waiting for connections...")
 
14
  try:
15
  # Receive the client's request (create a pair or connect to a key)
16
  request_type = await websocket.recv()
 
17
 
18
  if request_type == "create_pair":
19
  # Generate a unique key
 
30
  # Receive the key from the client
31
  key_to_connect = await websocket.recv()
32
 
33
+ # Check if the key exists in the database
34
  cursor.execute('SELECT client_ws FROM pairs WHERE key=?', (key_to_connect,))
35
+ result = cursor.fetchone()
36
+
37
+ if result:
38
+ other_client_ws = result[0]
39
+
40
+ # Pair the clients
41
+ await websocket.send(f"Connected to key {key_to_connect}. You are paired with {other_client_ws}")
42
+ await other_client_ws.send(f"Connected to key {key_to_connect}. You are paired with {str(websocket)}")
43
 
 
 
 
 
 
 
 
44
  else:
45
  # Inform the client that the key does not exist
46
  await websocket.send(f"Key {key_to_connect} does not exist.")
 
52
  # Generate a random key (you might want to implement a more robust key generation logic)
53
  return ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
54
 
55
+ start_server = websockets.serve(handle_client, "localhost", 8765)
56
 
57
  async def main():
58
  print("WebSocket server is running. Waiting for connections...")