File size: 1,553 Bytes
ef5d0f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, Request, WebSocket
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
import subprocess
import asyncio
import os

app = FastAPI()

# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")

# HTML endpoint
@app.get("/", response_class=HTMLResponse)
async def read_root():
    with open("static/index.html") as f:
        return f.read()

# WebSocket for emulator interaction
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    
    # Start emulator in headless mode
    emulator_process = subprocess.Popen(
        [
            "bash", "-c",
            "cd /opt/android-sdk/emulator && ./emulator -avd test -no-window -no-audio -gpu swiftshader_indirect -no-snapshot -qemu -vnc :0"
        ],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    
    # Start VNC to WebSocket proxy
    vnc_proxy = subprocess.Popen(
        ["websockify", "6080", "localhost:5900"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    
    try:
        while True:
            data = await websocket.receive_text()
            # Handle input commands if needed
            await websocket.send_text("Emulator is running at /vnc.html")
    except Exception as e:
        print(f"WebSocket error: {e}")
    finally:
        emulator_process.terminate()
        vnc_proxy.terminate()

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)