File size: 1,776 Bytes
0368bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useEffect, useRef, useState } from "react";


export default function App() {
const canvasRef = useRef(null);
const wsRef = useRef(null);
const [connected, setConnected] = useState(false);
const [playerPos, setPlayerPos] = useState([50, 50]);
const [status, setStatus] = useState("Disconnected");


useEffect(() => {
// Connect to backend WebSocket. When deploying to HF Spaces,
// use the absolute URL or relative path ("/ws").
const proto = window.location.protocol === "https:" ? "wss" : "ws";
const host = window.location.host; // same host as Space
const wsUrl = `${proto}://${host}/ws`;


const ws = new WebSocket(wsUrl);
wsRef.current = ws;


ws.addEventListener("open", () => {
setConnected(true);
setStatus("Connected");
// ask for initial frame
ws.send(JSON.stringify({ type: "noop" }));
});


ws.addEventListener("message", (ev) => {
const msg = JSON.parse(ev.data);
if (msg.type === "frame") {
drawFrame(msg.image_b64);
if (Array.isArray(msg.player_pos)) setPlayerPos(msg.player_pos);
} else if (msg.type === "status") {
setStatus(msg.text);
}
});


ws.addEventListener("close", () => {
setConnected(false);
setStatus("Disconnected");
});


return () => {
ws.close();
};
}, []);


// draw base64 png on canvas
function drawFrame(image_b64) {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext("2d");
const img = new Image();
img.onload = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
};
img.src = `data:image/png;base64,${image_b64}`;
}


// send action down the websocket
function sendAction(action) {
if (!wsRef.current || wsRef.current.readyState !== WebSocket.OPEN) return;
wsRef.current.send(JSON.stringify({ type: "action", action }));
}


}