Working on socket communications

This commit is contained in:
2025-09-21 10:20:18 -04:00
parent daaa0b5fee
commit 3951794ba9
2 changed files with 73 additions and 29 deletions

View File

@@ -663,6 +663,42 @@
console.error('Download error:', error);
alert('Download failed. Please try again.');
}
}
async function downloadGameStorage(gameId) {
const token = localStorage.getItem('authToken');
if (!token) {
showLogin();
return;
}
try {
const response = await fetch(`/download/${gameId}`, {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
// Get filename from Content-Disposition header, removing quotes and underscores
let filename = response.headers.get('Content-Disposition')?.split('filename=')[1] || 'game.zip';
filename = filename.replace(/^["\_]+|["\_]+$/g, ''); // Remove quotes and underscores from start and end
a.download = filename;
return { url: url, filename: filename };
} else if (response.status === 401) {
localStorage.removeItem('authToken');
showLogin();
} else {
alert('Download failed. Please try again.');
}
} catch (error) {
console.error('Download error:', error);
alert('Download failed. Please try again.');
}
}
async function runGame(gameId) {
const token = localStorage.getItem('authToken');
@@ -670,13 +706,26 @@
showLogin();
return;
}
const file = await downloadGameStorage(gameId);
const response = await fetch(file.url)
const arrayBuffer = await response.arrayBuffer();
const blob = new Blob([arrayBuffer],
{ type: response.headers.get(
'content-type') || 'application/octet-stream'
}
);
const _data = new FormData();
debugger;
_data.append('file', blob, ".zip");
try {
const response = await fetch(`/run/${gameId}`, {
const response = await fetch(`/run`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`
}
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
//body: JSON.stringify(file)
body: _data
});
if (response.ok) {