- JsBridge both ways, synchronous with the frames (emscripten pre/postMainLoop); hide/close delivered at once between frames (jumphp_events) - JumPHP\Gl\WebGl: 3D layer composed with the SDL picture (key colour / alpha), post-processing shaders, Mat4 (also software 3D on desktop) - JumPHP\Device\Lifecycle (hide/show/blur/focus/online/offline/resize/close, pause_scene, mute when hidden) and Device (vibrate, wake lock, orientation, fullscreen, clipboard, tilt/motion, battery) - JumPHP\Http: async requests (fetch / curl_multi / non-blocking sockets) - JumPHP\Device\Pwa + sw.js: installable, offline, updates (game.yaml pwa) - Gamepad: standard button names everywhere (pad0:a, pad0:start...), axes, hot-plug, rumble; Gamepad API in the browser - games/hello-webgl example, docs/api-web.md, AGENTS.md Co-Authored-By: Claude Opus 5.5 <[email protected]>
30 lines
1.6 KiB
JavaScript
30 lines
1.6 KiB
JavaScript
// Asynchronous HTTP for PHP (JumPHP\Http\Http): PHP sends {t: 'http', id, method, url, headers, body (base64),
|
|
// timeout}, the page runs fetch() and answers with a JsBridge 'http' event {id, status, headers, body (base64),
|
|
// url} or {id, error}. CORS applies: the server must allow the page's origin.
|
|
const toBase64 = (bytes) => {
|
|
let binary = '';
|
|
for (let i = 0; i < bytes.length; i += 0x8000) binary += String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000));
|
|
return btoa(binary);
|
|
};
|
|
const fromBase64 = (text) => Uint8Array.from(atob(text), (c) => c.charCodeAt(0));
|
|
|
|
export function install(api) {
|
|
api.on('http', (msg) => {
|
|
const controller = new AbortController();
|
|
const timer = setTimeout(() => controller.abort(), Math.max(1, msg.timeout || 20) * 1000);
|
|
const init = { method: msg.method, headers: msg.headers || {}, signal: controller.signal, redirect: 'follow' };
|
|
if (msg.body != null && msg.method !== 'GET' && msg.method !== 'HEAD') init.body = fromBase64(msg.body);
|
|
fetch(msg.url, init)
|
|
.then(async (response) => {
|
|
const body = new Uint8Array(await response.arrayBuffer());
|
|
const headers = {};
|
|
response.headers.forEach((value, name) => { headers[name] = value; });
|
|
api.send('http', { id: msg.id, status: response.status, headers, body: toBase64(body), url: response.url || msg.url });
|
|
})
|
|
.catch((e) => {
|
|
const why = e && e.name === 'AbortError' ? 'timeout' : (navigator.onLine === false ? 'offline' : 'network error or CORS');
|
|
api.send('http', { id: msg.id, error: why, url: msg.url });
|
|
})
|
|
.finally(() => clearTimeout(timer));
|
|
});
|
|
}
|