-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
79 lines (55 loc) · 1.54 KB
/
Copy pathServer.py
File metadata and controls
79 lines (55 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import asyncio
import websockets
import pyautogui
import socket
import qrcode
pyautogui.PAUSE = 0
pyautogui.FAILSAFE = False
PORT = 8765
# detect local IP automatically
def get_local_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
async def handler(websocket):
print("📱 Phone connected")
try:
async for message in websocket:
# ignore ping
if message == "ping":
continue
# ensure correct format
if ":" not in message:
continue
command, key = message.split(":", 1)
if command == "down":
pyautogui.keyDown(key)
elif command == "up":
pyautogui.keyUp(key)
except Exception as e:
print("⚠ Connection error:", e)
finally:
print("📴 Phone disconnected")
async def main():
ip = get_local_ip()
url = f"ws://{ip}:{PORT}"
print("🚀 WebSocket controller server running")
print("Listening on port", PORT)
print("Controller URL:", url)
# generate QR
qr = qrcode.make(url)
qr.show()
print("📷 Scan the QR code with the phone app")
async with websockets.serve(
handler,
"0.0.0.0",
PORT,
ping_interval=20,
ping_timeout=30,
):
await asyncio.Future()
asyncio.run(main())