-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconnectserver.py
More file actions
62 lines (52 loc) · 2.06 KB
/
reconnectserver.py
File metadata and controls
62 lines (52 loc) · 2.06 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
import websockets
import asyncio
import os
import time
from threading import Thread
from datetime import datetime
HOST = "127.0.0.1"
PORT = 8080
TIMEOUT = 120 # 2 minutes in seconds
last_message_time = time.time()
def debug_log(message):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{timestamp}] {message}")
async def handle_connection(websocket):
global last_message_time
client_ip = websocket.remote_address[0]
debug_log(f"New connection from {client_ip}")
try:
async for message in websocket:
last_message_time = time.time()
debug_log(f"Received message from {client_ip}: {message}")
await websocket.send("OK")
debug_log(f"Sent response to {client_ip}")
except websockets.exceptions.ConnectionClosed:
debug_log(f"Connection with {client_ip} closed")
except Exception as e:
debug_log(f"Error with {client_ip}: {str(e)}")
def check_timeout():
global last_message_time
while True:
current_time = time.time()
elapsed = current_time - last_message_time
debug_log(f"Checking timeout - Last ping: {elapsed:.1f}s ago")
if elapsed > TIMEOUT:
debug_log("❗ Timeout reached! Terminating Roblox and launching again...")
# Terminate Roblox first
os.system('su -c "am force-stop com.roblox.client"')
# Then launch Roblox
os.system('su -c \'am start -a android.intent.action.VIEW -d "roblox://placeID=9503261072"\'')
last_message_time = current_time # Reset timer
time.sleep(5)
async def start_server():
server = await websockets.serve(handle_connection, HOST, PORT)
debug_log(f"WebSocket server started on ws://{HOST}:{PORT}")
await server.wait_closed()
if __name__ == "__main__":
debug_log("Starting server...")
# Start timeout checker in background
Thread(target=check_timeout, daemon=True).start()
# Start WebSocket server
asyncio.get_event_loop().run_until_complete(start_server())
asyncio.get_event_loop().run_forever()