Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import websockets
|
| 3 |
+
import random
|
| 4 |
+
import string
|
| 5 |
+
import sqlite3
|
| 6 |
+
|
| 7 |
+
# Database setup (using SQLite for simplicity)
|
| 8 |
+
conn = sqlite3.connect('pairing_database.db')
|
| 9 |
+
cursor = conn.cursor()
|
| 10 |
+
cursor.execute('CREATE TABLE IF NOT EXISTS pairs (key TEXT, client_ws TEXT)')
|
| 11 |
+
conn.commit()
|
| 12 |
+
|
| 13 |
+
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 |
+
|
| 18 |
+
if request_type == "create_pair":
|
| 19 |
+
# Generate a unique key
|
| 20 |
+
unique_key = generate_key()
|
| 21 |
+
|
| 22 |
+
# Save the key and associate it with the current client in the database
|
| 23 |
+
cursor.execute('INSERT INTO pairs VALUES (?, ?)', (unique_key, str(websocket)))
|
| 24 |
+
conn.commit()
|
| 25 |
+
|
| 26 |
+
# Inform the client about the created key
|
| 27 |
+
await websocket.send(f"Pair key created: {unique_key}")
|
| 28 |
+
|
| 29 |
+
elif request_type == "connect_to_key":
|
| 30 |
+
# Receive the key from the client
|
| 31 |
+
key_to_connect = await websocket.recv()
|
| 32 |
+
|
| 33 |
+
# Retrieve all clients associated with the key from the database
|
| 34 |
+
cursor.execute('SELECT client_ws FROM pairs WHERE key=?', (key_to_connect,))
|
| 35 |
+
results = cursor.fetchall()
|
| 36 |
+
|
| 37 |
+
if results:
|
| 38 |
+
# Pair the current client with all clients associated with the key
|
| 39 |
+
for result in results:
|
| 40 |
+
other_client_ws = result[0]
|
| 41 |
+
if other_client_ws != str(websocket):
|
| 42 |
+
await websocket.send(f"Connected to key {key_to_connect}. You are paired with {other_client_ws}")
|
| 43 |
+
await other_client_ws.send(f"Connected to key {key_to_connect}. You are paired with {str(websocket)}")
|
| 44 |
+
else:
|
| 45 |
+
# Inform the client that the key does not exist
|
| 46 |
+
await websocket.send(f"Key {key_to_connect} does not exist.")
|
| 47 |
+
|
| 48 |
+
except websockets.exceptions.ConnectionClosed:
|
| 49 |
+
pass # Connection closed, handle appropriately
|
| 50 |
+
|
| 51 |
+
def generate_key():
|
| 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, "0.0.0.0", 7860)
|
| 56 |
+
|
| 57 |
+
async def main():
|
| 58 |
+
print("WebSocket server is running. Waiting for connections...")
|
| 59 |
+
await start_server
|
| 60 |
+
await asyncio.Event().wait()
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
asyncio.run(main())
|