délai côté php
This commit is contained in:
28
index.php
28
index.php
@@ -53,30 +53,34 @@
|
||||
const citation = document.createElement('blockquote');
|
||||
citation.innerText = formulaireData.get('question');
|
||||
conversation.appendChild(citation);
|
||||
|
||||
formulaire.reset();
|
||||
const requete = await fetch(formulaire.action, {
|
||||
method: formulaire.method,
|
||||
body: formulaireData
|
||||
});
|
||||
|
||||
const paragraphe = document.createElement('p');
|
||||
paragraphe.setAttribute('aria-busy', 'true');
|
||||
conversation.appendChild(paragraphe);
|
||||
const reponse = await requete.text();
|
||||
|
||||
const requete = await fetch(formulaire.action, {
|
||||
method: formulaire.method,
|
||||
body: formulaireData
|
||||
});
|
||||
paragraphe.setAttribute('aria-busy', 'false');
|
||||
Array.from(reponse).forEach((lettre, i) => {
|
||||
setTimeout(() => {
|
||||
|
||||
const reader = requete.body.getReader();
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
const lettre = decoder.decode(value, { stream: true });
|
||||
if (lettre == "\n") paragraphe.innerHTML += "<br>";
|
||||
else paragraphe.innerHTML += lettre;
|
||||
}, i * 40);
|
||||
});
|
||||
setTimeout(() => {
|
||||
await new Promise(r => setTimeout(r, 0)); // micro pause pour rendre l'UI responsive
|
||||
}
|
||||
|
||||
conversation.innerHTML += "<p>😸 Voulez-vous que je réponde à une autre question ?</p>";
|
||||
conversation.scrollTop = conversation.scrollHeight;
|
||||
bouton.disabled = false;
|
||||
bouton.setAttribute("aria-busy", false);
|
||||
}, reponse.length * 40);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
21
question.php
21
question.php
@@ -1 +1,20 @@
|
||||
<?php echo `python markov.py`; ?>
|
||||
<?php
|
||||
header('Content-Type: text/plain; charset=utf-8');
|
||||
header('Cache-Control: no-cache'); // pour éviter le buffering du navigateur
|
||||
header('X-Accel-Buffering: no'); // si nginx, pour désactiver le buffering
|
||||
|
||||
$reponse = `python markov.py`;
|
||||
|
||||
// désactiver le buffering PHP
|
||||
@ini_set('output_buffering', 'off');
|
||||
@ini_set('zlib.output_compression', 'off');
|
||||
while (ob_get_level()) ob_end_flush();
|
||||
flush();
|
||||
|
||||
// envoyer chaque lettre avec un délai
|
||||
$len = strlen($reponse);
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
echo $reponse[$i];
|
||||
flush(); // forcer l'envoi immédiat
|
||||
usleep(40000); // 40ms = 40000 microsecondes
|
||||
}
|
||||
Reference in New Issue
Block a user